The examples in this article describe how to use post requests in Android. Share to everyone for your reference. Specifically as follows:
One, need to use the scene
Using $.post () in jquery makes it easy to initiate a POST request, and sometimes you have to get some data from the server in the Android program, and you also have to use a POST request.
Ii. main categories to be used
The main classes to use for post requests in Android are HttpPost, HttpResponse, entityutils
Third, the main ideas
1, create the HttpPost instance, set the URL that needs to request the server.
2, set the parameters for the created HttpPost instance, and use the Namevaluepair class in the way that the key value pair is used when the parameter is set.
3. Initiate POST request get return instance HttpResponse
4, use Entityutils to return the value of the entity processing (can get the returned string, you can get the returned byte array)
The code is simpler, the notes are added, and they're posted directly.
Package com.justsy.url;
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";
The first step is to create the HttpPost object HttpPost httppost = new HttpPost (URL); The HTTP POST request parameters must be set with the Namevaluepair object 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 HttpResponse = null; try {//Set HttpPost request parameter httppost.setentity (new urlencodedformentity (params, HTTP).
Utf_8));
HttpResponse = new Defaulthttpclient (). Execute (httppost);
System.out.println (Httpresponse.getstatusline (). Getstatuscode ()); if (Httpresponse.getstatusline (). Getstatuscode () = = 200) {//step three, use the GetEntity method to live to return the results String result = Enti
Tyutils.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: Post requests using httpurlconnection:
String path = "Http://192.168.2.115:8080/android-web-server/httpConnectServlet.do?" PACKAGEID=89DCB664-50A7-4BF2-AEED-49C08AF6A58A ";
URL url = new URL (path);
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Conn.setrequestmethod ("POST");
Conn.setconnecttimeout (5000);
System.out.println (Conn.getresponsecode ());
I hope this article will help you with your Android program.