The Android post is transmitted via the HTTP protocol as follows:
Mode one: HttpPost (import Org.apache.http.client.methods.HttpPost)
The code is as follows:
Private Button Button1,button2,button3;
Private TextView TextView1;
Button1.setonclicklistener (New Button.onclicklistener () {
@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
Url?
String Uriapi = "http://www.dubblogs.cc:8751/Android/Test/API/Post/index.php";
String Uriapi = "Http://172.20.0.206:8082//TestServelt/login.do";
/* Create an HTTP POST connection */
HttpPost HttpRequest =new HttpPost (URIAPI);
Post operation transfer variables must be stored with namevaluepair[] Array
Request.getparameter ("name") method to get the parameter server
List <NameValuePair> params=new arraylist<namevaluepair> ();
Params.add (New Basicnamevaluepair ("name", "This is post"));
try{
Issue an HTTP request
Httprequest.setentity (New Urlencodedformentity (params,http. Utf_8));
Get HTTP response
HttpResponse httpresponse=new defaulthttpclient (). Execute (HttpRequest);
If the status code is OK
if (Httpresponse.getstatusline (). Getstatuscode () ==200) {
Remove the response string
String strresult=entityutils.tostring (Httpresponse.getentity ());
Textview1.settext (strresult);
}else{
Textview1.settext ("Error Response" +httpresponse.getstatusline (). toString ());
}
}catch (Clientprotocolexception e) {
Textview1.settext (E.getmessage (). toString ());
E.printstacktrace ();
} catch (Unsupportedencodingexception e) {
Textview1.settext (E.getmessage (). toString ());
E.printstacktrace ();
} catch (IOException e) {
Textview1.settext (E.getmessage (). toString ());
E.printstacktrace ();
}
}
});
Mode two: HttpURLConnection, URL (import java.net.httpurlconnection;import java.net.url;import java.net.URLEncoder;)
private void HttpURLConnection () {
try{
String Pathurl = "Http://172.20.0.206:8082/TestServelt/login.do";
Establish a connection
URL url=new url (pathurl);
HttpURLConnection httpconn= (httpurlconnection) url.openconnection ();
Setting connection Properties
Httpconn.setdooutput (TRUE);//Use URL connection for output
Httpconn.setdoinput (TRUE);//Enter using a URL connection
Httpconn.setusecaches (false);//Ignore cache
Httpconn.setrequestmethod ("POST");//Set URL request method
String requeststring = "Client side wants to stream data to the server ...";
Set request Properties
To obtain data byte data, the encoding of the request data stream must be consistent with the encoding of the server-side processing request stream below
byte[] requeststringbytes = requeststring.getbytes (encoding_utf_8);
Httpconn.setrequestproperty ("Content-length", "" "+ requeststringbytes.length);
Httpconn.setrequestproperty ("Content-type", "Application/octet-stream");
Httpconn.setrequestproperty ("Connection", "keep-alive");//Maintain long connections
Httpconn.setrequestproperty ("Charset", "UTF-8");
//
String Name=urlencoder.encode ("Huang Kung Fu", "Utf-8");
Httpconn.setrequestproperty ("name", name);
Build the output stream and write the data
OutputStream outputstream = Httpconn.getoutputstream ();
Outputstream.write (requeststringbytes);
Outputstream.close ();
Get response status
int responsecode = Httpconn.getresponsecode ();
if (HTTPURLCONNECTION.HTTP_OK = = Responsecode) {//connection succeeded
Process data when correctly responding
StringBuffer sb = new StringBuffer ();
String ReadLine;
BufferedReader Responsereader;
Processing the response stream, which must be consistent with the encoding of the server response stream output
Responsereader = new BufferedReader (New InputStreamReader (Httpconn.getinputstream (), encoding_utf_8));
while ((ReadLine = Responsereader.readline ()) = null) {
Sb.append (ReadLine). Append ("\ n");
}
Responsereader.close ();
Tv.settext (Sb.tostring ());
}
}catch (Exception ex) {
Ex.printstacktrace ();
}
}
Android via HTTP protocol post transmission mode