码迷,mamicode.com
首页 > 其他好文 > 详细

AsyncTask异步任务类使用学习

时间:2015-06-17 19:55:37      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

new MyAsyncTask()
.execute("http://pic.baike.soso.com/p/20120716/bki-20120716095331-640956396.jpg");

 

class MyAsyncTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected void onPreExecute() {
// TODO 自动生成的方法存根
super.onPreExecute();
dialog.show();

}

@Override
protected Bitmap doInBackground(String... params) {

Bitmap bitmap = null;
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet(params[0]);
try {
HttpResponse respon = httpClient.execute(httpPost);

if (respon.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = respon.getEntity();
byte[] data = EntityUtils.toByteArray(entity);
bitmap = BitmapFactory
.decodeByteArray(data, 0, data.length);

}
else {
}
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}

@Override
protected void onPostExecute(Bitmap result) {
// TODO 自动生成的方法存根
super.onPostExecute(result);
imageView.setImageBitmap(result);
dialog.dismiss();

}

}

AsyncTask异步任务类使用学习

标签:

原文地址:http://www.cnblogs.com/lzh-Linux/p/4584095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!