Not much nonsense to say, directly on the code comparison is real.
In fact, it is also a record of their own way, for the future of lazy preparation .... Don't hit me, I'm a lazy man ~
Send parameters based on the Get method of the HTTP protocol:
/** * send GET request * @param path request path * @param params Request Parameters * @param encoding encoding * @return request succeeded */private static boolean sendgetrequest (String path,map<string, string> params, string coding) throws Exception{ //Stitching url stringbuilder url = New stringbuilder (path); url.append ("?"); for (Map.entry<string, string> entry : params.entryset ()) { url.append (Entry.getkey ()). Append ("="); url.append (Urlencoder.encode (Entry.getvalue (), coding)); url.append ("&"); } url.deletecharat (Url.length () - 1); //Creating a connection httpurlconnection conn = (httpurlconnection) New url (url.tostring ()). OpenConnection (); Conn.setconnecttimeout, conn.setrequestmethod ("GET"); if ( Conn.getresponsecode () == 200) { return true; } return false;}
Post method based on HTTP protocol send parameters:
/** * send POST request * @param path request path * @param params request Parameters * @param encoding encoding * @return Request Success */private static Boolean sendpostrequest (string path, map<string, string> params, string encoding) throws exception{ //encapsulated into time=60&name=ceshishipin Such data stringbuilder data = new stringbuilder (); if (Params!=null && !params.isempty ()) { for (Map.Entry<String, string> entry : params.entryset ()) { Data.append (Entry.getkey ()). Append ("="); data.append ( Urlencoder.encode (Entry.getvalue (), encoding)); Data.append ("&"); &Nbsp; } data.deletecharat (Data.length () - 1); } byte[] entity = data.tostring (). GetBytes ();// Generate Entity Data HttpURLConnection conn = (httpurlconnection) new url ( Path). OpenConnection (); conn.setconnecttimeout (; ) Conn.setrequestmethod ("POST"); conn.setdooutput (true);//Allow external output data //set the properties of the request content-type content-length these two are necessary etc to cooike Or the session should also set the corresponding property conn.setrequestproperty ("Content-type", "application/ X-www-form-urlencoded "); conn.setrequestproperty (" Content-length ", String.valueOf ( entity.length)); outputstream outstream = conn.getoutputstream (); outstream.write (entity); &NBsp; if (Conn.getresponsecode () == 200) { return true; } return false;}
method of sending data: (Here I only send two parameters)
/** * sending data * @param name parameters one * @param time Parameters two * @return success * @throws exception */public static boolean send (string name, string time) throws exception{// TODO Auto-generated method stub String url = " The path you requested "; map<string, string> params = new hashmap< String, string> (); params.put ("name", name); Params.put ("Time", time); try { return sendhttpclientpostrequest (url, params, "UTF-8"); } catch (exception e) { e.printstacktrace (); } rEturn false;}
Usage in activity or using threads to update the UI
Public void send (VIEW&NBSP;V) { final string name = Edit1.gettext (). toString (); final string time = edit2.gettext (). ToString (); final handler handler = new handler () { public void handlemessage (message msg) { switch (msg.what) { case 0: Toast.maketext (Getapplicationcontext (), r.string.success, 1) .show (); break; case 1: &nBsp; toast.maketext (Getapplicationcontext (), r.string.error, 1) .show (); break; default: break; } } }; new thread (New Runnable () { @Override public void run () { try { boolean result = videoservice.send (name,time); &nBsp;if (Result) { handler.sendemptymessage (0); } else { handler.sendemptymessage (1); } } catch (exception e) { e.printstacktrace (); } } }). Start ();}
Here I do not use the button listener event directly in the Main.xml configuration: android:onclick= "send"//Use the Send method above
Look online says there is a ready-made framework (httpClient) that can be used:
/** * send post requests via httpclient * @param path request Path * @param params Request Parameters * @param encoding encoding * @return Request succeeded */private static boolean sendhttpclientpostrequest (string path, map<string, string> params, string encoding) throws Exception{ List< Namevaluepair> pairs = new arraylist<namevaluepair> ();//Store Request Parameters if (Params!=null && !params.isempty ()) { for (Map.Entry<String, string> entry : params.entryset ()) { Pairs.add (New basicnamevaluepair (Entry.getkey (), entry.getvalue ())); } } urlencodedformentity entity = new urlencodedformentiTy (pairs, encoding); //Generating Entity Data HttpPost httpPost = new HttpPost (path); httppost.setentity (entity); defaulthttpclient client = new defaulthttpclient (); //simulates a browser behavior Httpresponse response = client.execute (HttpPost); if ( Response.getstatusline (). Getstatuscode () == 200) { Return true; } return false;}
sending a GET request via HttpClient is not written by itself and can be see sending a POST request via httpclient. All right, I'm the lazy one. Take the copy change directly from here. (*^__^*) hehe ...
Android series five (send parameters via get or post to web App)