In the previous article, org. apache. commons. but the android jar package has been integrated with Org. apache. HTTP. client. httpclient class, so this article mainly introduces httpclient in Android to send get and post requests:
The following is a post:
This article describes the Apache httpclient module integrated with the android SDK. Note that the Apache httpclient module here is httpclient 4.0 (Org. Apache. http. *) instead of Jakarta commons httpclient 3.x( org. Apache. commons. httpclient .*).
Two important classes are used in the httpclient module: httpget and httppost. These two classes are used to submit http get and http post requests respectively. To test the example in this section, you must first compile a ServletProgramTo receive http get and
Http post request. You can also use other server resources to test the examples in this section.
Suppose 192.168.17.81 is the IP address of the local machine. The client can access server resources through the following URL:
Http: // 192.168.17.81: 8080/querybooks/queryservlet? Bookname = Development
Here, bookname is the request parameter of queryservlet, indicating the name of the book. This parameter is used to query the book information.
Now we need to submit the request information to queryservlet through the httpget and httppost classes, and display the returned results in the textview component.
Httpget or httppost must be used to access HTTP resources in the following three steps.
1. Create an httpget or httppost object and pass in the http get or httppost object through the construction method of the requested URL.
2. Use the execute method of the defaulthttpclient class to send an http get or http post request and return the httpresponse object.
3. Return the response information through the getentity method of the httpresponse interface and perform corresponding processing.
If you use the httppost method to submit an http post request, you also need to use the setentity method of the httppost class to set the request parameters.
In this example, two buttons are used to submit http get and http post requests respectively, obtain the value of the request parameter (bookname) from the edittext component, and finally display the returned results in the textview component. The two buttons share an onclick event method,
CodeAs follows:
Public void onclick (view) {// you need to replace the IP address in this example with the IP address string URL of your machine = "http: // 192.168.17.81: 8080/querybooks/queryservlet "; textview tvqueryresult = (textview) findviewbyid (R. id. tvqueryresult); edittext etbookname = (edittext) findviewbyid (R. id. etbookname); httpresponse = NULL; try {Switch (view. GETID () {// submit the http get request case R. id. btngetquery: // Add the request parameter URL + = "? Bookname = "+ etbookname. gettext (). tostring (); // Step 2: Create an httpget object httpget = new httpget (URL); // Step 3: Use the execute method to send an http get request, return the httpresponse object httpresponse = new defaulthttpclient(cmd.exe cute (httpget); // determine the request response status code. The status code 200 indicates that the server successfully responded to the client request if (httpresponse. getstatusline (). getstatuscode () = 200) {// step 3rd: Use the getentity method to obtain the returned result string result = entityutils. tostring (httpresponse. getentity (); // remove the "\ r" character from the returned result; otherwise, a small square tvqueryresult is displayed after the result string. settext (result. replaceall ("\ r", "");} break; // submit an http post request case R. id. btnpostquery: // Step 2: 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 http post request parameter httppost. setentity (New urlencodedformentity (Params, HTTP. utf_8); // Step 2: Use the execute method to send an http post request and return the httpresponse object httpresponse = new defaulthttpclient(cmd.exe cute (httppost); If (httpresponse. getstatusline (). getstatuscode () = 200) {// step 3rd: Use the getentity method to obtain the returned result string result = entityutils. tostring (httpresponse. getentity (); // remove the "\ r" character from the returned result; otherwise, a small square tvqueryresult is displayed after the result string. settext (result. replaceall ("\ r", "") ;}break ;}} catch (exception e) {tvqueryresult. settext (E. getmessage ());}