public abstract class AsyncTask<Params,Progress,Result>{...}
public class MainActivity extends Activity {
// 程序入口
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyAsyncTask my = new MyAsyncTask();
my.execute("http://photocdn.sohu.com/20110927/Img320705637.jpg");
}
}
package com.sun.asynctask;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
/**
* 异步任务,实现网页内容的获取
*
*
* 生成该类的对象,并调用execute方法之后
*
* 首先执行的是onProExecute() 方法,
*
* 其次执行的doInBackground()方法
*/
public class MyAsyncTask extends AsyncTask<String, Integer, Bitmap> {
/**
* 在execute() 方法执行后立即执行,运行在UI线程中,
* 在后台任务开始前执行,用于标识UI界面
*/
protected void onPreExecute() {
super.onPreExecute();
Log.i("msg","onPreExecute()...");
}
/**
* 后台执行耗时的任务;
*
* 方法中的 String 参数对应 AsyncTask的第一个参数;
* 返回的 Bitmap 对应的是AsyncTask 的第三个参数;
*
* 该方法并不运行在UI线程中,主要用于异步操作,可以调用publishProgress()方法触发
* onProgressUpdate对UI进行操作;
*
*/
protected Bitmap doInBackground(String... params) {
Log.i("msg","doInBackground(String... params)...");
try {
/* 网络访问方式 二 */
/*
URL url = new URL(params[0]);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.connect(); // 开始连接
int zong = connection.getContentLength();
InputStream is2 = connection.getInputStream();
*/
/* 开始网络访问数据 */
HttpGet hg = new HttpGet(params[0]); // 此处注意参数的用法
HttpClient hc = new DefaultHttpClient();
HttpResponse hr = hc.execute(hg); // 发送请求,得到响应
// 判断请求是否成功
if(hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
Log.i("msg", "access success...");
HttpEntity he = hr.getEntity();
InputStream is = he.getContent(); // 获取输入流对象,好比搭桥
long total = he.getContentLength(); // 文件的总字节数
ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 输出流,临时容器,用于装从is中流出的数据
byte[] buffer = new byte[1024]; // 缓存容器,每次装载1024 个字节数据
int len = 0; // 每次读的字节数
int curLen = 0 ; // 已读多少数据
while((len=is.read(buffer))!=-1){ // 当len !=-1 时,也就是还有数据可读
Log.i("msg","begin read data..."+len+",total:"+total);
baos.write(buffer, 0, len); // 向临时容器中装数据
curLen=curLen+len; // 更新已读的数据
/* 在UI显示当前读取的进度 , 调用次方法触发onProgressUpdate() 方法执行 */
publishProgress((int)(((float)curLen/total)*100));
}
Bitmap bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, (int)total);
is.close();
return bitmap;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 括号中的参数:String 对应的是AsyncTask 的第三个参数,也就是
* 接收了 从doInBackground() 返回的结果;
* 此方法在 doInBackground() 方法执行结束后执行,运行在UI线程中,
* 可以对UI进行更新
*/
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
Log.i("msg","onPostExecute(String result)..."+result.getHeight());
}
/**
* 方法括号中的Integer 对应AsyncTask 中的第二个参数;
* 在doInBackground() 中每次调用publishProgress() 时被执行;
* 该方法是在UI线程中的,所以可以用于对UI进行更新
*/
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Log.i("msg","onProgressUpdate(Integer... values)..."+values[0]);
}
/**
* 图片的下载
*/
public HttpURLConnection downPic(String urltemp){
try {
URL url = new URL(urltemp); // 确定连接地址
// 打开一个连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect(); // 开始连接
return connection;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有