Android-http Connection Get/post Request

Source: Internet
Author: User

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

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.