Android via HTTP protocol post transmission mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.