Android submits data to the server in two ways four ways

Source: Internet
Author: User

Android application development, will often submit data to the server and from the server to obtain data, this article mainly gives the use of HTTP protocol httpclient way to submit data to the server method.


/** * @author Dylan * This class encapsulates two ways to submit data to a Web server in Android Four ways */public class Submitdatabyhttpclientandordinaryway {/** * use get please To submit the data in an ordinary way * @param map passes in the data, encapsulated in the form of map * @param path requires the address of the server servlet * @return return a Boolean parameter of type * @throws Exception * /public Boolean submitdatabydoget (map<string, string> Map, String path) throws Exception {//Patchwork request address StringBuilder s b = 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 ()///Get the request without setting anything dooutput () or something like that? if (httpconn.getresponsecode () = = HTTPURLCONNECTION.HTTP_OK) {return true;} return false;} /** * Normal way Dopost request submission data * @param map to pass in the data, in the form of a map package * @param path requires the address of the server servlet * @return Returns a Boolean parameter of type * @throws Exception */public boolean submitdatabydopost (map<string, string> Map, String p Ath) throws Exception {//Note that there is no parameter in the post address, so Newurl should be careful not to add the following parameter URL url = new URL (path),//Post method when the parameters and URLs are submitted separately, The Parameter form is this: name=y&age=6stringbuilder 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 package * @param path requires the address of the server servlet * @return returned by the Boolean type of Parameters * @throws Exception */public Boolean submitdatabyhttpclientdoget (map<string, string> Map, String path) throws Exc eption {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 server in HttpClient dopost mode * @param map to pass in the data, in the form of a map package * @param path requires the address of the server servlet * @return returned by the Boolean type of Parameter * @throws Exception */public Boolean submintdatabyhttpclientdopost (map<string, STRing> map, String path) throws Exception {//1. Get a Browser object equivalent to HttpClient, use the implementation class of this interface to create the object, Defaulthttpclienthttpclient HC = new Defaulthttpclient ();//Dopost request when the request is set, the key is the path HttpPost request = new HttpPost (path);//2. Sets 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 ());p Arameters.add (namevaluepairs); The request entity httpentity is also an interface, and we use its implementation class urlencodedformentity to create objects, note that the arguments to the following string type are used to specify the encoded httpentity entity = new Urlencodedformentity (Parameters, "UTF-8"); request.setentity (entity);//3. Execution Request HttpResponse response = Hc.execute (request);//4. Use the return code to determine if the request succeeds if (Response.getstatusline (). Getstatuscode () = = HTTPURLCONNECTION.HTTP_OK) {return true;} return false;}}


Android submits data to the server in two ways four ways

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.