The example in this article describes how Android uses HttpPost to send requests to the server. Share to everyone for your reference, specific as follows:
Import java.util.List;
Import org.apache.http.HttpEntity;
Import Org.apache.http.HttpResponse;
Import Org.apache.http.NameValuePair;
Import org.apache.http.client.HttpClient;
Import org.apache.http.client.entity.UrlEncodedFormEntity;
Import Org.apache.http.client.methods.HttpPost;
Import org.apache.http.conn.ConnectTimeoutException;
Import org.apache.http.impl.client.DefaultHttpClient;
Import Org.apache.http.params.CoreConnectionPNames;
Import Org.apache.http.protocol.HTTP;
Import Org.apache.http.util.EntityUtils;
Import Android.util.Log;
public class Requestbyhttppost {public static String time_out = ' Operation timeout ';
public static string DoPost (list<namevaluepair> params,string url) throws exception{String result = null;
New HttpPost object HttpPost httppost = new HttpPost (URL); Set the character set httpentity entity = new Urlencodedformentity (params, HTTP.
UTF_8);
Set parameter entity httppost.setentity (entity); Get HttpClient object HttpClient httpclient = new DefaulThttpclient ();
Connection Timeout Httpclient.getparams (). Setparameter (Coreconnectionpnames.connection_timeout, 30000);
Request Timeout Httpclient.getparams (). Setparameter (Coreconnectionpnames.so_timeout, 30000);
try {//Get HttpResponse instance HttpResponse Httpresp = Httpclient.execute (HttpPost); Judgment is enough to request success if (Httpresp.getstatusline (). Getstatuscode () = = 200) {//get returned data result = ENTITYUTILS.T
Ostring (Httpresp.getentity (), "UTF-8");
LOG.I ("HttpPost", "HttpPost method request success, return data as follows:");
LOG.I ("result", result);
else {log.i ("HttpPost", "HttpPost Mode request Failed");
The catch (Connecttimeoutexception e) {result = Time_out;
return result;
}
}
A complete class that can be used directly.
I hope this article will help you with the Android program.