Today we are going to learn how to use the network communication interface provided by Apache for Android, and if you want to use the HTTP protocol, you need to use HttpClient.
Using HttpClient is simpler than using httpurlconnection, let's take a look at the code:
1. Get the network picture using the Get method:
Call:
Mbtnget.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {Bitmap picture = Getnetpicture ("http://192.168.1.19:8080/Web1/img.jpg"); if (null! = picture) {mimg.setimagebitmap (picture);}});
Method:
private Bitmap getnetpicture (String imgurl) {Bitmap img = null; HttpClient client = null; HttpGet request = null; HttpResponse response = null;try {request = new HttpGet (imgurl); client = new Defaulthttpclient (); Client.getparams (). Setparameter (coreconnectionpnames.connection_timeout, 1000 * 5); Link Timeout client.getparams (). Setparameter (coreconnectionpnames.so_timeout,1000 * 5); Read timeout response = Client.execute (Request), if (Response.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) { InputStream is = Response.getentity (). getcontent (); If it is a returned string, you can use Entityutils directly to handle//entityutils.tostring (Response.getentity ()); img = BITMAPF Actory.decodestream (IS);} else {showtoast ("Get the network picture Failed!");}} catch (Clientprotocolexception e) {e.printstacktrace (); Showtoast (E.getmessage ());} catch (IOException e) { E.printstacktrace (); Showtoast (E.getmessage ());} return img;}
Using the Get method is simple, if you want to add parameters can be added directly after the URL? par=??? &par2=??? To set the parameters. Here we request a picture on the local server with no parameters set.
Steps to use:
(1) Get a HttpClient object, using HttpClient client = new Defaulthttpclient (); Get
(2) Set timeout parameters (preferably set, otherwise if not visited for a long time)
(3) New a HttpGet object, and attach the URL to it. HttpGet request = new HttpGet (URL);
(4) Obtain the Response object: HttpResponse response = Client.excute (request);
(5) Determine if the status code of the response is successful, and if successful, the stream object can be further obtained. Response.getentiry (). GetContent () to get the stream object, once you get the stream, you can do whatever you want.
2. Use the Post method to get the network HTML file:
Call:
Mbtnpost.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {String content = Getnethtml ("http://192.168.1.19:8080/Web1/login3.jsp"); Mtv.settext (Content!=null content: "Get HTML failed!");});
Method:
private string getnethtml (string url) {HttpClient client = null; HttpPost request = null; HttpResponse response = null; String data = Null;client = new Defaulthttpclient (); request = new HttpPost (URL); list<namevaluepair> params = new arraylist<namevaluepair> ();p Arams.add (New Basicnamevaluepair ("par", " Httpclinet_android_post ")), try {httpentity entity = new Urlencodedformentity (params," gb2312 "); Request.setentity ( entity), Client.getparams (). Setparameter (Coreconnectionpnames.connection_timeout, 1000*5); Client.getparams (). Setparameter (Coreconnectionpnames.so_timeout, 1000*5); response = Client.execute (request); if (HTTPSTATUS.SC_OK = = Response.getstatusline (). Getstatuscode ()) {data = Entityutils.tostring (Response.getentity ());} else{}} catch (Clientprotocolexception e) {e.printstacktrace (); Showtoast ("" +e.getmessage ());} catch (IOException e) {e.printstacktrace (); Showtoast ("" +e.getmessage ());} return data;}
Steps to use:
Compared to the Get method, the Post method always has to set some parameters, so it is more complicated, but also very simple.
(1) Get HttpClient object, set timeout ...
(2) Get the HttpPost object and pass in the URL.
(3) Set the parameters to be passed. The Post method uses the Namevaluepair to set the parameters for the POST request. This is set to a parameter par with a value of: Httpclient_android_post
list<namevaluepair> params = new arraylist<namevaluepair> ();p Arams.add (New Basicnamevaluepair ("par", " Httpclinet_android_post ") <pre name=" code "class=" java ">httpentity entity = new Urlencodedformentity (params," gb2312 "); <pre name=" code "class=" Java ">request.setentity (entity);
(4) The next step is the same as get.
Operating effect:
Code Address:
http://download.csdn.net/detail/u013647382/8267145
Android Getting Started note-network communication-HttpClient