转自百度文档 : 读取网页内容 更新进度
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
public class PageTask extends AsyncTask<String, Integer, String>{
//可变长的输入参数 , 与AsyncTask.exucute()对应
@Override
protected String doInBackground(String... params) {
try {
HttpClient client = new DefaultHttpClient();
//params[0] 表示连接的url
HttpGet get = new HttpGet(params[0]);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
InputStream is = entity.getContent();
String s = null;
if(is != null){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[4*32];
int ch = -1;
int count = 0;
while((ch = is.read(buf)) != -1){
baos.write(buf, 0, ch);
count += ch;
if(length > 0 ){
//如果知道 相应的长度,调用publishprogress() 更新
publishProgress((int)((count / (float) length)*100));
}
Thread.sleep(100);
}
s = new String(baos.toByteArray());
//返回结果
return s;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPostExecute(String result) {
//返回HTML 页面的内容
// message.setText(result);
}
@Override
protected void onPreExecute() {
//super.onPreExecute();
//任务启动 , 可以在这里显示一个对话框 , 这里简单处理
//message.setText(R.string.task_started);
}
@Override
protected void onProgressUpdate(Integer... values) {
// super.onProgressUpdate(values);
//更新进度
//message.setText(values[0]);
}
}
PageTask task = new PageTask();
task.execute(url.getText().toString());