GET/POST requests for Android-HTTP connections
The Apache httpclient (Org. Apache. http. *) module is provided in the android SDK. This module involves two important classes: httpget and httppost.
Creation steps: 1. Create an httpget (or httppost) object and pass the URL to be requested to an httpget (or httppost) object through the constructor; 2. Use the execute method of the defaulthttpclient class to send an http get or http post request and return the httpresponse object. 3. Use the getentity method of the httpresponse interface to return the response information. Although both are implemented in this way, there are some differences between the two. The specific code is as follows: http get request: String URL; // step 1, create an httpget object httpget = new httpget (URL); // step 2, use the execute method to send an http get request and return the httpresponse object httpresponse = new defaulthttpclient(cmd.exe cute (httpget ); if (httpresponse. getstatusline (). getstatuscode () = 200) {// step 3, use the getentity method to return the result string result = entityutils. tostring (httpresponse. getentity ();} http post request: String URL; // Step 1: Create an httppost object httppost = new httppost (URL ); // set the http post request parameters using the namevaluepair Object List <namevaluepair> Params = new arraylist <namevaluepair> (); Params. add (New basicnamevaluepair ("bookname", etbookname. gettext (). tostring (); // sets the httppost request parameter httppost. setentity (New urlencodedformentity (Params, HTTP. utf_8); // step 2, use the execute method to send an http get request and return the httpresponse object httpresponse = new defaulthttpclient(cmd.exe cute (httppost); If (httpresponse. getstatusline (). getstatuscode () = 200) {// step 3, use the getentity method to return the result string result = entityutils. tostring (httpresponse. getentity ();} The above is an explanation of the get and post methods. There are similarities and differences between the two.