In Android application development, data is often submitted to and from the server. This article mainly provides a method of submitting data to the server using the http protocol using the HttpClient method.
The code is relatively simple, so I won’t go into too much explanation here, just look at the code.
/** * @author Dylan * This class encapsulates two ways to submit data to web servers in Android. */ public class SubmitDataByHttpClientAndOrdinaryWay { /** * Submit data in a normal way using get request * @param map The data passed in is encapsulated in the form of a map * @param path Requires the address of the server servlet * @return The parameter of the boolean type returned * @throws Exception */ public Boolean submitDataByDoGet(Map<String, String> map, String path) throws Exception { // Patch the request address StringBuilder sb = new StringBuilder(path); ("?"); for (<String, String> entry : ()) { (()).append("=").append(()); ("&"); } (() - 1); String str = (); (str); URL Url = new URL(str); HttpURLConnection HttpConn = (HttpURLConnection) (); ("GET"); (5000); // Do you don’t need to set DoOutPut() or something like that when requesting GET? if (() == HttpURLConnection.HTTP_OK) { return true; } return false; } /** * Normal DoPost request to submit data * @param map The data passed in is encapsulated in the form of a map * @param path Requires the address of the server servlet * @return The parameter of the boolean type returned * @throws Exception */ public Boolean submitDataByDoPost(Map<String, String> map, String path) throws Exception { // Note that there are no parameters in the Post address, so when newURL, you should be careful not to add the following parameters URL Url = new URL(path); // When submitting Post method, the parameters and URL are submitted separately. The parameter form is as follows: name=y&age=6 StringBuilder sb = new StringBuilder(); // ("?"); for (<String, String> entry : ()) { (()).append("=").append(()); ("&"); } (() - 1); String str = (); HttpURLConnection HttpConn = (HttpURLConnection) (); ("POST"); (5000); (true); ("Content-Type", "application/x-www-form-urlencoded"); ("Content-Length", (().length)); OutputStream os = (); (()); if (() == HttpURLConnection.HTTP_OK) { return true; } return false; } /** * Send request data to the server through the DoGet method of HttpClient * @param map The data passed in is encapsulated in the form of a map * @param path Requires the address of the server servlet * @return The parameter of the boolean type returned * @throws Exception */ public Boolean submitDataByHttpClientDoGet(Map<String, String> map, String path) throws Exception { HttpClient hc = new DefaultHttpClient(); // Request path StringBuilder sb = new StringBuilder(path); ("?"); for (<String, String> entry : ()) { (()).append("=").append(()); ("&"); } (() - 1); String str = (); (str); HttpGet request = new HttpGet(()); HttpResponse response = (request); if (().getStatusCode() == HttpURLConnection.HTTP_OK) { return true; } return false; } /** * Submit data to the server in the DoPost mode of HttpClient * @param map The data passed in is encapsulated in the form of a map * @param path Requires the address of the server servlet * @return The parameter of the boolean type returned * @throws Exception */ public Boolean submintDataByHttpClientDoPost(Map<String, String> map, String path) throws Exception { // 1. Obtain a browser object equivalent to HttpClient, use the implementation class of this interface to create the object, DefaultHttpClient HttpClient hc = new DefaultHttpClient(); // Set the request when requesting DoPost, the key is the path HttpPost request = new HttpPost(path); // 2. Set request parameters for the request, that is, the parameters to be uploaded to the web server List<NameValuePair> parameters = new ArrayList<NameValuePair>(); for (<String, String> entry : ()) { NameValuePair nameValuePairs = new BasicNameValuePair((), ()); (nameValuePairs); } // The request entity HttpEntity is also an interface. We use its implementation class UrlEncodedFormEntity to create an object. Note that the following String parameter is used to specify the encoding. HttpEntity entity = new UrlEncodedFormEntity(parameters, "UTF-8"); (entity); // 3. Execute the request HttpResponse response = (request); // 4. Use the return code to determine whether the request is successful or not if (().getStatusCode() == HttpURLConnection.HTTP_OK) { return true; } return false; } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.