GET/POST requests for Android-HTTP connections

Source: Internet
Author: User

 

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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.