The Apache HttpClient (org.apache.http.*) module is available in the Android SDK. There are two important classes involved in this module: HttpGet and HttpPost.
To create a step:1. Create the HttpGet (or HttpPost) object, and the URL to be requested is passed into the HttpGet (or HttpPost) object by constructing the method;2. Use the Execute method of the Defaulthttpclient class to send an HTTP GET or HTTP POST request and return the HttpResponse object;3, the response information is returned through the GetEntity method of the HttpResponse interface. Although both are implemented in this way, there are some differences between the two, the exact code is as follows: HTTP GET request: String URL; The first step is to create the HttpGet object HttpGet httpget = new HttpGet (URL); The second step is to use the Execute method to send an HTTP GET request and return the HttpResponse object httpresponse = new Defaulthttpclient (). Execute (httpget); if (Httpresponse.getstatusline (). Getstatuscode () = = 200) {//Third step, use the GetEntity method to return the result String results = Entit Yutils.tostring (Httpresponse.getentity ()); HTTP POST request: String URL; The first step is to create the HttpPost object HttpPost httppost = new HttpPost (URL);//Set HTTP POST request parameter must use Namevaluepair objectList params = new ArrayList ();Params.add (New Basicnamevaluepair ("BookName", Etbookname.gettext (). toString ()));//Set HttpPost request Parametershttppost.setentity (New urlencodedformentity (params, HTTP. Utf_8));The second step is to use the Execute method to send an HTTP GET request and return the HttpResponse object httpresponse = new Defaulthttpclient (). Execute (httppost); if (Httpresponse.getstatusline (). Getstatuscode () = = 200) {//Third step, use the GetEntity method to return the result String results = Entit Yutils.tostring (Httpresponse.getentity ()); }
Android-http Connection Get/post Request