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
- Public String doget ()
- {
- String Uriapi = "http://XXXXX?str=I+am+get+String";
- String result= "";
- HttpGet httprequst = new HttpGet (URI Uri);
- HttpGet httprequst = new HttpGet (String URI);
- Creates a httpget or HttpPost object, passing the requested URL through a constructor method to the HttpGet or HttpPost object.
- HttpGet httprequst = new HttpGet (URIAPI);
- New Defaulthttpclient (). Execute (httpurirequst requst);
- try {
- //Use the Execute method of the Defaulthttpclient class to send an HTTP GET request and return the HttpResponse object.
- HttpResponse HttpResponse = new Defaulthttpclient (). Execute (httprequst); Where HttpGet is a subclass of Httpurirequst
- if (Httpresponse.getstatusline (). Getstatuscode () = = ( )
- {
- Httpentity httpentity = httpresponse.getentity ();
- result = Entityutils.tostring (httpentity); //Remove answer string
- //Remove extra characters in general
- Result.replaceall ("\ r", ""); Remove the "\ r" character from the returned result, or a small square will appear after the result string
- }
- Else
- Httprequst.abort ();
- } catch (Clientprotocolexception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- result = E.getmessage (). toString ();
- } catch (IOException e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- result = E.getmessage (). toString ();
- }
- return result;
- }
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
- Public String DoPost ()
- {
- String Uriapi = "Http://XXXXXX";//post mode no arguments here
- String result = "";
- HttpPost httprequst = new HttpPost (URIAPI); Create a HttpPost object
- List <NameValuePair> params = new arraylist<namevaluepair> ();
- Params.add (new Basicnamevaluepair ("str", "I am Post String"));
- try {
- Httprequst.setentity (new Urlencodedformentity (params,http. Utf_8));
- HttpResponse HttpResponse = new Defaulthttpclient (). Execute (httprequst);
- if (Httpresponse.getstatusline (). Getstatuscode () = = ( )
- {
- Httpentity httpentity = httpresponse.getentity ();
- result = Entityutils.tostring (httpentity); //Remove answer string
- }
- } catch (Unsupportedencodingexception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- result = E.getmessage (). toString ();
- }
- catch (Clientprotocolexception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- result = E.getmessage (). toString ();
- }
- catch (IOException e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- result = E.getmessage (). toString ();
- }
- return result;
- }
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
- Httpparams httpparameters = new Basichttpparams ();
- Httpconnectionparams.setconnectiontimeout (httpparameters, 10*); Set Request Timeout 10 seconds
- Httpconnectionparams.setsotimeout (httpparameters, 10*1000); //Set wait data timeout 10 seconds
- Httpconnectionparams.setsocketbuffersize (params, 8192);
- HttpClient HttpClient = new Defaulthttpclient (httpparameters); //The parameter is passed in when constructing defaulthttpclient
- Add permissions to the network connection in Androidmanifest.xml because it is networked
- <uses-permission android:name="Android.permission.INTERNET"/>
HttpGet and HttpPost of the HttpClient module