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 commonly used in the HttpGet and HttpPost these two classes, respectively corresponding to the Get and post methods.
Whether you are using HttpGet or using HttpPost, you must access the HTTP resources by following 3 steps.
1. Create a HttpGet or HttpPost object that will pass the URL of the request 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. Return the response information through the GetEntity method of the HttpResponse interface and handle it accordingly.
If you submit an HTTP POST request using the HttpPost method, you need to set the request parameters using the Setentity method of the HttpPost class. Parameters must be stored with the namevaluepair[] array.
Some examples are given below:
Get Way:
//HttpGet mode request public static void Requestbyhttpget () throws Exception {String path = ' https://reg.163.c
Om/logins.jsp?id=helloworld&pwd=android ";
New HttpGet object HttpGet httpget = new HttpGet (path);
Gets the HttpClient object httpclient httpclient = new Defaulthttpclient ();
Get HttpResponse instance HttpResponse Httpresp = Httpclient.execute (HttpGet); Judgment is enough to request success if (Httpresp.getstatusline (). Getstatuscode () = = http_200) {//Get returned data String
result = Entityutils.tostring (Httpresp.getentity (), "UTF-8");
LOG.I (Tag_httpget, "HttpGet the way the request succeeds, returns the data as follows:");
LOG.I (tag_httpget, result);
else {log.i (Tag_httpget, "HttpGet method request Failed"); }
}
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 that passes the URL of the request to the HttpGet or HttpPost object by constructing the method.
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 (HT Tpresponse.getstatusline () getstatuscode () = httpentity httpentity = Httprespo
Nse.getentity (); result = Entityutils.tostring (httpentity);//Remove answer string//generally delete extra characters result.replaceal L ("\ R", "");//Remove the "\ r" character in the return result, otherwise a small square is displayed 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; }