Package Com.example.android_asynctask;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 Org.apache.http.util.entityutils;import Android.os.asynctask;import Android.os.bundle;import Android.app.activity;import Android.app.ProgressDialog; Import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.view.menu;import Android.view.view;import Android.widget.button;import Android.widget.ImageView; Public classMainactivity extends Activity {Privatebutton button; PrivateImageView ImageView; PrivateString image_path ="http://e.hiphotos.baidu.com/image/pic/item/2fdda3cc7cd98d10b510fdea233fb80e7aec9021.jpg"; PrivateProgressDialog Dialog; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button=(Button) Findviewbyid (R.id.button1); ImageView=(ImageView) Findviewbyid (R.ID.IMAGEVIEW1); Dialog=NewProgressDialog ( This); Dialog.settitle ("Prompt Information"); Dialog.setmessage ("downloading, please wait ..."); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method Stub Newmytask (). Execute (image_path); } }); } /** * Rules using Asynchronous tasks: 1, declared class inheritance Asynctask callout three parameter type * 2, the first parameter represents the task to be performed, usually the path of the network, the second parameter represents the scale of the progress, and the third parameter represents the return result of the task execution * * @author Liende **/ Public classMyTask extends Asynctask<string, Void, bitmap> { /** * Indicates action before task execution*/@Overrideprotected voidOnPreExecute () {//TODO auto-generated Method StubSuper.onpreexecute (); Dialog.show (); } /** * Mainly to complete time-consuming operation*/@OverrideprotectedBitmap doinbackground (String ... arg0) {//TODO auto-generated Method Stub//using the network connection class httpclient the extraction of network data from a class of Wang ChengHttpClient HttpClient =Newdefaulthttpclient (); HttpGet HttpGet=NewHttpGet (arg0[0]); Bitmap Bitmap=NULL; Try{HttpResponse HttpResponse=Httpclient.execute (HttpGet); if(Httpresponse.getstatusline (). Getstatuscode () = = $) {httpentity httpentity=httpresponse.getentity (); byte[] data =Entityutils.tobytearray (httpentity); Bitmap=bitmapfactory. Decodebytearray (Data,0, data.length); } } Catch(Exception e) {//Todo:handle Exception } returnbitmap; } /** * Mainly update the UI operation*/@Overrideprotected voidOnPostExecute (Bitmap result) {//TODO auto-generated Method StubSuper.onpostexecute (Result); Imageview.setimagebitmap (result); Dialog.dismiss (); }} @Override Publicboolean Oncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}