- /**
- * Two ways to submit data to a Web server in Android four ways
- */
- Public class Submitdatabyhttpclientandordinaryway {
- /**
- * Use GET request to submit data in normal way
- * @param map to pass in the data, in the form of a map encapsulated
- * @param path requires the address of the server servlet
- * @return The parameters of the Boolean type returned
- * @throws Exception
- */
- Public Boolean Submitdatabydoget (map<string, string> Map, String Path) throws Exception {
- //Patchwork a request 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);
- if (httpconn.getresponsecode () = = HTTPURLCONNECTION.HTTP_OK) {
- return true;
- }
- return false;
- }
- /**
- * Dopost request submission data in normal mode
- * @param map to pass in the data, in the form of a map encapsulated
- * @param path requires the address of the server servlet
- * @return The parameters of the Boolean type returned
- * @throws Exception
- */
- Public Boolean Submitdatabydopost (map<string, string> Map, String Path) throws Exception {
- //Note that there is no parameter in the post address, so be careful not to add the following parameters when Newurl
- URL url = new URL (path);
- //Post method when the parameter and URL are submitted separately, the Parameter form is like 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 data to the server in HttpClient doget mode
- * @param map to pass in the data, in the form of a map encapsulated
- * @param path requires the address of the server servlet
- * @return The parameters of the Boolean type returned
- * @throws Exception
- */
- Public Boolean Submitdatabyhttpclientdoget (map<string, string> Map, String Path) throws Exception { /c4>
- 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 server in HttpClient Dopost way
- * @param map to pass in the data, in the form of a map encapsulated
- * @param path requires the address of the server servlet
- * @return The parameters of the Boolean type returned
- * @throws Exception
- */
- Public Boolean Submintdatabyhttpclientdopost (map<string, string> Map, String Path) throws Exception {
- ///1. Obtain a httpclient equivalent to the browser object, using the implementation class of this interface to create the object, Defaulthttpclient
- HttpClient HC = new defaulthttpclient ();
- ///Dopost request is set at the time of request, the key is the path
- HttpPost request = new HttpPost (path);
- //2. Set the request parameters 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 (Entry.getkey (), Entry.getvalue ());
- Parameters.Add (Namevaluepairs);
- }
- //Request entity httpentity is also an interface, we use its implementation class urlencodedformentity to create objects, note that later a string type of argument 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. To determine the success of a request by a return code
- if (Response.getstatusline (). Getstatuscode () = = HTTPURLCONNECTION.HTTP_OK) {
- return true;
- }
- return false;
- }
- }
Original from http://luecsc.blog.51cto.com/2219432/1111923
4 ways Android sends data to Web server