Android submits data to the server in four ways
In Android application development, data is often submitted to the server and obtained from the server. This article mainly describes how to use the http protocol to submit data to the server in HttpClient mode.
/*** @ Author Dylan * This class encapsulates four methods for submitting data to the web server in Android */public class SubmitDataByHttpClientAndOrdinaryWay {/*** use get request submit data in Normal Mode * @ param map the data passed in, encapsulate * @ param path in the form of map to require the server servlet address * @ return boolean type parameter * @ throws Exception */public Boolean submitDataByDoGet (Map
Map, String path) throws Exception {// piece together the request address StringBuilder sb = new StringBuilder (path); sb. append ("? "); For (Map. Entry
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); // do not set DoOutPut () or something like GET? If (HttpConn. getResponseCode () = HttpURLConnection. HTTP_ OK) {return true;} return false;}/*** common DoPost request to submit data * @ param map passed in data, encapsulate * @ param path in the form of map to require the server servlet address * @ return boolean type parameter * @ throws Exception */public Boolean submitDataByDoPost (Map
Map, String path) throws Exception {// note that parameters are not included in the Post address, so note that the following parameter URL = new Url (path) cannot be added in newURL ); // parameters and URLs are submitted separately in Post mode. The parameter format is as follows: name = y & age = 6 StringBuilder sb = new StringBuilder (); // sb. append ("? "); For (Map. Entry
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;}/*** use the HttpClient DoGet method to send data to the server. * @ param map refers to the data transmitted, encapsulated in map form * @ param path requires the server servlet address * @ return boolean type parameter * @ throws Exception */public Boolean submitDataByHttpClientDoGet (Map
Map, String path) throws Exception {HttpClient hc = new DefaultHttpClient (); // Request path StringBuilder sb = new StringBuilder (path); sb. append ("? "); For (Map. Entry
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.exe cute (request); if (response. getStatusLine (). getStatusCode () = HttpURLConnection. HTTP_ OK) {return true;} return false;}/*** submit data to the server in the form of HttpClient DoPost * @ param map, encapsulated in map form * @ param path requires the server servlet address * @ return boolean type parameter * @ throws Exception */public Boolean submintDataByHttpClientDoPost (Map
Map, String path) throws Exception {// 1. get an HttpClient equivalent to a browser object. Use the implementation class of this interface to create an object. Set the request when DefaultHttpClientHttpClient hc = new DefaultHttpClient (); // set the request in DoPost mode, the key is the path HttpPost request = new HttpPost (path); // 2. set Request Parameters for the request, that is, the List of parameters to be uploaded to the web server.
Parameters = new ArrayList
(); For (Map. Entry
Entry: map. entrySet () {NameValuePair nameValuePairs = new BasicNameValuePair (entry. getKey (), entry. getValue (); parameters. add (nameValuePairs);} // The Request Entity HttpEntity is also an interface. We use its implementation class UrlEncodedFormEntity to create objects, note that the next String type parameter is used to specify the encoded HttpEntity entity = new UrlEncodedFormEntity (parameters, "UTF-8"); request. setEntity (entity); // 3. execute the request HttpResponse response = hc.exe cute (request); // 4. the return code is used to determine whether the request is successful if (response. getStatusLine (). getStatusCode () = HttpURLConnection. HTTP_ OK) {return true;} return false ;}}