异步类下载 带进度条

package com.shamusoft.asynctask;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {
String URL = "http://60.28.186.211/lyn/blue.zip";

TextView tv;

TextView tv2;

ProgressBar pb;

Handler _handler;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tv = (TextView) this.findViewById(R.id.tv);
tv2 = (TextView) this.findViewById(R.id.tv2);
pb = (ProgressBar) this.findViewById(R.id.firstBar);

_handler = new Handler(){

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
tv2.setText("服务器异常");
}};

Masynctask task = new Masynctask();
task.execute(URL);
}

@SuppressWarnings("rawtypes")
public class Masynctask extends AsyncTask {

@Override
protected Object doInBackground(Object... params) {
Log.i("XX", "doInBackground");
try {
downLoad(URL);
} catch (Exception e) {
_handler.sendEmptyMessage(0);
// throw new RuntimeException();
}
return null;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
// 任务启动,可以在这里显示一个对话框,这里简单处理
pb.setVisibility(0);
tv.setText("开始下载");
Log.i("XX", "onPreExecute");
}

@Override
protected void onPostExecute(Object result) {
tv.setText("下载完毕");
Log.i("XX", "onPostExecute");
}

@Override
protected void onProgressUpdate(Object... values) {
// 更新进度
pb.setProgress((Integer) values[0]);
tv2.setText("下载进度:" + values[0] + "%");
}

@SuppressWarnings("unchecked")
public void downLoad(String url) throws ClientProtocolException, IOException {
HttpClient client = getHttpClient();
HttpGet httpGet = new HttpGet(url);
int pro = 0;

HttpResponse httpResponse = client.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
int code = httpResponse.getStatusLine().getStatusCode();
Log.i("XX","code:"+code);

float length = entity.getContentLength();
InputStream is = entity.getContent();
if (is != null) {
File file = new File(
android.os.Environment
.getExternalStorageDirectory()
+ "/blue.zip");
if (file.exists()) {
file.delete();
}
file.createNewFile();

FileOutputStream out = new FileOutputStream(file);
byte[] buf = new byte[128];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
count += ch;
if (length > 0) {
pro = (int) (count * 100 / length);
publishProgress(pro);
}
out.write(ch);
}
is.close();
out.close();
}

}

private static final int REQUEST_TIMEOUT = 10 * 1000;// 设置请求超时10秒钟
private static final int SO_TIMEOUT = 10 * 1000; // 设置等待数据超时时间10秒钟

public HttpClient getHttpClient() {
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
REQUEST_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpParams);
return client;
}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值