HttpClient provides Android developers with a simple way to operate the HTTP network connection, there are two ways to connect the process, get and post, first look at how to implement
The default is Get mode
//first put the parameters into list , and then URL-encode the parameters list<basicnamevaluepair> params = new linkedlist<basicnamevaluepair> (); Params.add (New Basicnamevaluepair ("param1", "China")); Params.add (New Basicnamevaluepair ("param2", "value2")); BASEURL String baseUrl = "http://www.baidu.com"; HttpGet GetMethod = new HttpGet (BaseUrl + "?" + param) by stitching the URL with the parameters. HttpClient HttpClient = new Defaulthttpclient (); try {httpresponse response = Httpclient.execute (GetMethod);//Initiate GET request LOG.I (TAG, "Rescode =" + response.gets Tatusline (). Getstatuscode ()); Get the response code LOG.I (TAG, "result =" + entityutils.tostring (response.getentity (), "Utf-8"));//Get server response content} catch (Client Protocolexception e) {//TODO auto-generated catch block E.printstacktrace (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }
Post mode
In the same way as GET, the parameter is first placed in the list params = new linkedlist<basicnamevaluepair> (); Params.add (New Basicnamevaluepair ("param1", "POST Method")); Params.add (New Basicnamevaluepair ("Param2", "second parameter"); try {httppost Postmethod = new HttpPost (BASEURL); Postmethod.setentity (New urlencodedformentity (params, "utf-8")); Fill in the number of references in post entity HttpResponse response = Httpclient.execute (Postmethod); Run the Post method log.i (TAG, "Rescode =" + Response.getstatusline (). Getstatuscode ()); Get the response code LOG.I (TAG, "result =" + entityutils.tostring (response.getentity (), "Utf-8")); Get response content} catch (Unsupportedencodingexception e) {//TODO auto-generated catch block E.prin Tstacktrace (); } catch (Clientprotocolexception e) {//TODO auto-generated catch block E.printstacktrace (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }
Suppose you need to update the UI after acquiring the network resources, you need to use the asynchronous way, or it will occur incorrectly
Handler Handler = new Handler () {@Override public void Handlemessage (Message msg) {if (msg.what = = 0x123) {tv.settext (result); } } }; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); TV = (TextView) Findviewbyid (r.id.tv); result = ""; Final HttpClient HttpClient = new Defaulthttpclient (); New Thread () {public void run () {HttpGet HttpRequest = new HttpGet ("HT Tp://www.baidu.com "); try {HttpResponse HttpResponse = Httpclient.execute (HttpRequest); if (Httpresponse.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {//Get the returned string result = Entityutils.tostring (httpresponse.getentity ()); Tv.settext(result);//Assume that the error message msg = new Message () is used here; Msg.what = 0x123; Handler.sendmessage (msg); }} catch (Clientprotocolexception e) {//TODO auto-generated catch block E.printstacktrace (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }}}.start (); }
Android using HttpClient method and error-prone problem