From http://www.2cto.com/kf/201207/138589.html
I. Applicable scenarios
You can use $. Post () in jquery to initiate a POST request conveniently.ProgramYou must also use the POST request to obtain some data from the server.
Ii. Main types to be used
In Android, the main classes used for post requests are httppost, httpresponse, and entityutils.
Iii. Main Ideas
1. Create an httppost instance and set the URL of the server to be requested.
2. set parameters for the created httppost instance. The namevaluepair class is used when key-value pairs are used for parameter settings.
3. Initiate a POST request to obtain the returned instance httpresponse
4. Use entityutils to process the returned object (the returned string or byte array can be obtained)
CodeIt is also relatively simple, and the comments are added, so they are directly posted.
Import Java. Io. ioexception; Import Java. util. arraylist; Import Java. util. List; Import Org. Apache. http. httpresponse; Import Org. Apache. http. namevaluepair; Import Org. Apache. http. Client. clientprotocolexception; Import Org. Apache. http. Client. entity. urlencodedformentity; Import Org. Apache. http. Client. Methods. httppost; Import Org. Apache. http. impl. Client. defaulthttpclient; Import Org. Apache. http. Message. basicnamevaluepair; Import Org. Apache. http. Protocol. HTTP; Import Org. Apache. http. util. entityutils; Import Android. App. activity; Import Android. OS. Bundle; Public Class Httpurlactivity Extends Activity {@ override Public Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate); system. Out. println ( "Start URL ..." ); String URL = "Http: // 192.168.2.112: 8080/justsyapp/applet" ; // Step 1: Create an httppost object Httppost = New Httppost (URL ); // The namevaluepair object must be used to set the http post request parameters. List <namevaluepair> Params = New Arraylist <namevaluepair> (); Params. Add ( New Basicnamevaluepair ("action", "downloadandroidapp" ); Params. Add ( New Basicnamevaluepair ("packageid", "89dcb664-50a7-4bf2-aeed-49c08af6a58a" ); Params. Add ( New Basicnamevaluepair ("UUID", "test_ok1" ); Httpresponse =Null ; Try { // Set httppost Request Parameters Httppost. setentity ( New Urlencodedformentity (Params, HTTP. utf_8); httpresponse = New Defaulthttpclient(cmd.exe cute (httppost ); // System. Out. println (httpresponse. getstatusline (). getstatuscode ()); If (Httpresponse. getstatusline (). getstatuscode () = 200 ){ // Step 3: Use the getentity method to return results String result = Entityutils. tostring (httpresponse. getentity (); system. Out. println ( "Result:" + Result); T. displaytoast (httpurlactivity. This , "Result:" + Result );}} Catch (Clientprotocolexception e) {e. printstacktrace ();} Catch (Ioexception e) {e. printstacktrace ();} system. Out. println ( "End URL ..." ); Setcontentview (R. layout. Main) ;}} Add: Use httpurlconnection for post request string path = "Http: // 192.168.2.115: 8080/Android-web-server/httpconnectservlet. do? Packageid = 89dcb664-50a7-4bf2-aeed-49c08af6a58a" ; URL = New URL (PATH); httpurlconnection Conn = (Httpurlconnection) URL. openconnection (); Conn. setrequestmethod ( "Post" ); Conn. setconnecttimeout ( 5000 ); System. Out. println (conn. getresponsecode ());