Android sdk 3.0 编译通过
package com.download;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.URL;
import java.URLConnection;
import org.apache.http.client.ClientProtocolException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidDownLoadActivity extends Activity
{
private Button DownloadTextButton = null;
private Button DownloadMp3Button = null;
ProgressBar pb;
TextView tv;
int fileSize;
int downLoadFileSize;
String fileEx, fileNa, filename;
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{// 定义一个Handler,用于处理下载线程与UI间通讯
if (!Thread.currentThread().isInterrupted())
{
switch (msg.what) {
case 0:
pb.setMax(fileSize);
case 1:
pb.setProgress(downLoadFileSize);
int result = downLoadFileSize * 100 / fileSize;
tv.setText(result + "%");
break;
case 2:
Toast.makeText(AndroidDownLoadActivity.this, "文件下载完成", 1)
.show();
break;
case -1:
String error = Data().getString("error");
Toast.makeText(AndroidDownLoadActivity.this, error, 1)
.show();
break;
}
}
super.handleMessage(msg);
}
};
/*
* down load file to sdcard.
*/
public void down_file(String url, String path) throws IOException
{
// 下载函数
filename = url.substring(url.lastIndexOf("/") + 1);
// 获取文件名
URL myURL = new URL(url);
URLConnection conn = myURL.openConnection();
t();
InputStream is = InputStream();
this.fileSize = ContentLength();// 根据响应获取文件大小
Log.i("value", "length = " + this.fileSize);
if (this.fileSize <= 0)
{
throw new RuntimeException("无法获知文件大小 ");
}
if (is == null)
{
throw new RuntimeException("stream is null");
}
File file = new ExternalStorageDirectory(),
filename);
FileOutputStream fos = new FileOutputStream(file);
// 把数据存入路径+文件名
byte buf[] = new byte[1024];
downLoadFileSize = 0;
sendMsg(0);
do
{
// 循环读取
int numread = is.read(buf);
if (numread == -1)
connect下载 {
break;
}
fos.write(buf, 0, numread);
downLoadFileSize += numread;
sendMsg(1);// 更新进度条
} while (true);
sendMsg(2);// 通知下载完成
try
{
is.close();
} catch (Exception ex)
{
Log.e("tag", "error: " + ex.getMessage(), ex);
}
}
private void sendMsg(int flag)
{
Message msg = new Message();
msg.what = flag;
handler.sendMessage(msg);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
Create(savedInstanceState);
setContentView(R.layout.main);
Create(savedInstanceState);
setContentView(R.layout.main);
pb = (ProgressBar) findViewById(R.id.down_pb);
tv = (TextView) findViewById(R.id.tv);
new Thread()
{
public void run()
{
try
{
down_file(
"下载路径 ",
"/sdcard/");
// 下载文件,参数:第一个URL,第二个存放路径
} catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
DownloadTextButton = (Button) findViewById(R.id.DownTextButtonId);
DownloadTextButton.setOnClickListener(new DownTextButtonListener());
DownloadMp3Button = (Button) findViewById(R.id.DownMp3ButtonId);
DownloadMp3Button.setOnClickListener(new DownMp3ButtonListener());
}
private class DownTextButtonListener implements OnClickListener
{
public void onClick(View v)
{
Thread thread = new Thread(new Runnable()
{
public void run()
{
// TODO Auto-generated method stub
HttpDownloader httpDownloader = new HttpDownloader();
String irc = httpDownloader
.download("下载路径 ",);
System.out.println(irc);
}
});
thread.start();
}
}
private class DownMp3ButtonListener implements OnClickListener
{
public void onClick(View v)
{
Thread thread = new Thread(new Runnable()
{
public void run()
{
// TODO Auto-generated method stub
HttpDownloader httpDownloader = new HttpDownloader();
int result = httpDownloader
.downFile(
"下载路径 ",,
"test/", "");
System.out.println(result);
}
});
thread.start();
}
}
}
package com.download;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论