In the development of Android application, we often submit data to the server and get data from the server, this paper mainly gives the method of submitting data to the server using the HTTP protocol in HttpClient way.
The code is relatively simple, here do not go too much elaboration, directly look at the code.
/** * @author Dylan * This class encapsulates two ways to submit data to a Web server in Android four methods */public class Submitdatabyhttpclientandordinaryway {/** * Use a GET request to submit data in a normal manner * @param map passes in data that is encapsulated in the form of a map * @param path requires the address of the server servlet * @return The Boolean type parameter to return * @throws Exception/Public Boolean submitdatabydoget (map<string, string> Map, String path) throws Exception {//Patchwork of requests
Address StringBuilder sb = new StringBuilder (path);
Sb.append ("?"); For (map.entry<string, string> entry:map.entrySet ()) {Sb.append (Entry.getkey ()). Append ("="). Append (
Entry.getvalue ());
Sb.append ("&");
} Sb.deletecharat (Sb.length ()-1);
String str = sb.tostring ();
System.out.println (str);
URL url = new URL (str);
HttpURLConnection httpconn = (httpurlconnection) url.openconnection ();
Httpconn.setrequestmethod ("get");
Httpconn.setreadtimeout (5000);
Get-way requests do not have to set anything dooutput () or something like that?
if (httpconn.getresponsecode () = = HTTPURLCONNECTION.HTTP_OK) {return true;
return false;
/** * Normal mode of Dopost request submission data * @param the data passed in map, encapsulated in the form of map * @param path requires the address of the server servlet * @return The Boolean type parameter returned * @throws Exception/Pub Lic Boolean submitdatabydopost (map<string, string> Map, String path) throws Exception {//Note that the post address is without parameters, so Newurl
Should be careful not to add the following parameter URL url = new URL (path);
When the Post method is submitted, the parameter and URL are submitted separately, and the Parameter form is this: name=y&age=6 StringBuilder sb = new StringBuilder ();
Sb.append ("?"); For (map.entry<string, string> entry:map.entrySet ()) {Sb.append (Entry.getkey ()). Append ("="). Append (
Entry.getvalue ());
Sb.append ("&");
} Sb.deletecharat (Sb.length ()-1);
String str = sb.tostring ();
HttpURLConnection httpconn = (httpurlconnection) url.openconnection ();
Httpconn.setrequestmethod ("POST");
Httpconn.setreadtimeout (5000);
Httpconn.setdooutput (TRUE);
Httpconn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
Httpconn.setrequestproperty ("Content-length", String.valueof (Str.getbytes (). Length)); OutputStream OS = httpconn.getoutputstReam ();
Os.write (Str.getbytes ());
if (httpconn.getresponsecode () = = HTTPURLCONNECTION.HTTP_OK) {return true;
return false; /** * Send the data to the server in a httpclient doget mode @param the data that is passed in the map, encapsulated in the form of map * @param the address of the server servlet * @return The returned Bo Olean type parameter * @throws Exception/public Boolean submitdatabyhttpclientdoget (map<string, string> Map, String path
) throws Exception {httpclient HC = new Defaulthttpclient ();
Request path StringBuilder SB = new StringBuilder (path);
Sb.append ("?"); For (map.entry<string, string> entry:map.entrySet ()) {Sb.append (Entry.getkey ()). Append ("="). Append (
Entry.getvalue ());
Sb.append ("&");
} Sb.deletecharat (Sb.length ()-1);
String str = sb.tostring ();
System.out.println (str);
HttpGet request = new HttpGet (sb.tostring ());
HttpResponse response = Hc.execute (request);
if (Response.getstatusline (). Getstatuscode () = = HTTPURLCONNECTION.HTTP_OK) {return true;
return false; /** * Submit data to the service in a httpclient dopost mannerServer * @param map of the data passed in, encapsulated in the form of a map * @param path requires the address of the servers servlet * @return The Boolean type parameter returned * @throws Exception * * Public Boolean submintdatabyhttpclientdopost (map<string, string> Map, String path) throws Exception {//1. Get an equivalent
Browser object httpclient, using the implementation class of this interface to create objects, defaulthttpclient httpclient HC = new Defaulthttpclient ();
Dopost mode request, the key is the path HttpPost request = new HttpPost (path); 2.
Sets the request parameter for the request, which is the parameter that will be uploaded to the Web server list<namevaluepair> parameters = new arraylist<namevaluepair> (); For (map.entry<string, string> entry:map.entrySet ()) {Namevaluepair namevaluepairs = new Basicnamevaluepair (ent
Ry.getkey (), Entry.getvalue ());
Parameters.Add (Namevaluepairs); }//Request entity httpentity is also an interface, we use its implementation class urlencodedformentity to create an object, note that a string type parameter is used to specify the encoding of the httpentity entity = new
Urlencodedformentity (Parameters, "UTF-8");
Request.setentity (entity); 3.
Execution Request HttpResponse response = Hc.execute (request); 4. The return code is used to determine if the request is successful or not (Response.getstatusline (). Getstatuscode () = = HTTPURLCONNECTION.HTTP_OK) {return true;
return false;
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.