HttpGet and HttpPost of the HttpClient module

Source: Internet
Author: User

The Android SDK integrates the Apache httpclient module. Note that the Apache HttpClient module here is HttpClient 4.0 (org.apache.http.*), rather than the common Jakarta Commons HttpClient 3. X (org.apache.commons.httpclient.*).

HttpClient Common HttpGet and HttpPost these two classes, respectively, corresponding to get mode and post mode.

Whether you are using HttpGet or using HttpPost, you must access HTTP resources through the following 3 steps.

1. Create a HttpGet or HttpPost object and pass the requested URL to 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 by the GetEntity method of the HttpResponse interface and processed accordingly.

If you use the HttpPost method to submit an HTTP POST request, you need to set the request parameters using the Setentity method of the HttpPost class. Parameters must be stored in the namevaluepair[] array.

HttpGet

[Java]View PlainCopy
  1. Public String doget ()
  2. {
  3. String Uriapi = "http://XXXXX?str=I+am+get+String";
  4. String result= "";
  5. HttpGet httprequst = new HttpGet (URI Uri);
  6. HttpGet httprequst = new HttpGet (String URI);
  7. Creates a httpget or HttpPost object, passing the requested URL through a constructor method to the HttpGet or HttpPost object.
  8. HttpGet httprequst = new HttpGet (URIAPI);
  9. New Defaulthttpclient (). Execute (httpurirequst requst);
  10. try {
  11. //Use the Execute method of the Defaulthttpclient class to send an HTTP GET request and return the HttpResponse object.
  12. HttpResponse HttpResponse = new Defaulthttpclient (). Execute (httprequst); Where HttpGet is a subclass of Httpurirequst
  13. if (Httpresponse.getstatusline (). Getstatuscode () = = ( )
  14. {
  15. Httpentity httpentity = httpresponse.getentity ();
  16. result = Entityutils.tostring (httpentity); //Remove answer string
  17. //Remove extra characters in general
  18. Result.replaceall ("\ r", ""); Remove the "\ r" character from the returned result, or a small square will appear after the result string
  19. }
  20. Else
  21. Httprequst.abort ();
  22. } catch (Clientprotocolexception e) {
  23. //TODO auto-generated catch block
  24. E.printstacktrace ();
  25. result = E.getmessage (). toString ();
  26. } catch (IOException e) {
  27. //TODO auto-generated catch block
  28. E.printstacktrace ();
  29. result = E.getmessage (). toString ();
  30. }
  31. return result;
  32. }

HttpPost

If you use the HttpPost method to submit an HTTP POST request, you need to set the request parameters using the Setentity method of the HttpPost class. Parameters must be stored in the namevaluepair[] array.

[Java]View PlainCopy
  1. Public String DoPost ()
  2. {
  3. String Uriapi = "Http://XXXXXX";//post mode no arguments here
  4. String result = "";
  5. HttpPost httprequst = new HttpPost (URIAPI); Create a HttpPost object
  6. List <NameValuePair> params = new arraylist<namevaluepair> ();
  7. Params.add (new Basicnamevaluepair ("str", "I am Post String"));
  8. try {
  9. Httprequst.setentity (new Urlencodedformentity (params,http.  Utf_8));
  10. HttpResponse HttpResponse = new Defaulthttpclient (). Execute (httprequst);
  11. if (Httpresponse.getstatusline (). Getstatuscode () = = ( )
  12. {
  13. Httpentity httpentity = httpresponse.getentity ();
  14. result = Entityutils.tostring (httpentity); //Remove answer string
  15. }
  16. } catch (Unsupportedencodingexception e) {
  17. //TODO auto-generated catch block
  18. E.printstacktrace ();
  19. result = E.getmessage (). toString ();
  20. }
  21. catch (Clientprotocolexception e) {
  22. //TODO auto-generated catch block
  23. E.printstacktrace ();
  24. result = E.getmessage (). toString ();
  25. }
  26. catch (IOException e) {
  27. //TODO auto-generated catch block
  28. E.printstacktrace ();
  29. result = E.getmessage (). toString ();
  30. }
  31. return result;
  32. }



To send a connection request, you need to set parameters such as link timeout and request timeout, or it will stop or crash for a long time.

[Java]View PlainCopy
    1. Httpparams httpparameters = new Basichttpparams ();
    2. Httpconnectionparams.setconnectiontimeout (httpparameters, 10*); Set Request Timeout 10 seconds
    3. Httpconnectionparams.setsotimeout (httpparameters, 10*1000); //Set wait data timeout 10 seconds
    4. Httpconnectionparams.setsocketbuffersize (params, 8192);
    5. HttpClient HttpClient = new Defaulthttpclient (httpparameters); //The parameter is passed in when constructing defaulthttpclient
    6. Add permissions to the network connection in Androidmanifest.xml because it is networked
    7. <uses-permission android:name="Android.permission.INTERNET"/>

HttpGet and HttpPost of the HttpClient module

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.