Android post transmission over HTTP
Article category: mobile development
Android post transmission over HTTP is as follows:
Method 1: 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 Scheme
// 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 in the namevaluepair [] Array
// Request. getparameter ("name ")
List <namevaluepair> Params = new arraylist <namevaluepair> ();
Params. Add (New basicnamevaluepair ("name", "this is Post "));
Try {
// Send an HTTP request
Httprequest. setentity (New urlencodedformentity (Params, HTTP. utf_8 ));
// Get http Response
Httpresponse = new defaulthttpclient(cmd.exe cute (httprequest );
// If the status code is 200 OK
If (httpresponse. getstatusline (). getstatuscode () = 200 ){
// Retrieve 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 ();
}
}
});
Method 2: httpurlconnection and 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 ();
//// Set Connection Properties
Httpconn. setdooutput (true); // use a URL to connect to the output
Httpconn. setdoinput (true); // use URL Connection for Input
Httpconn. setusecaches (false); // ignore Cache
Httpconn. setrequestmethod ("Post"); // sets the URL request Method
String requeststring = "the data that the client needs to send to the server as a stream ...";
// Set Request attributes
// Obtain the Data byte. the encoding of the request data stream must be consistent with the encoding of the Request stream processed by the following server.
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 a persistent connection
Httpconn. setrequestproperty ("charset", "UTF-8 ");
//
String name = urlencoder. encode ("Huang Wuyi", "UTF-8 ");
Httpconn. setrequestproperty ("name", name );
// Create an output stream and write data
Outputstream = httpconn. getoutputstream ();
Outputstream. Write (requeststringbytes );
Outputstream. Close ();
// Obtain the response status
Int responsecode = httpconn. getresponsecode ();
If (httpurlconnection. http_ OK = responsecode) {// connection successful
// Process data when the response is correct
Stringbuffer sb = new stringbuffer ();
String Readline;
Bufferedreader responsereader;
// Process 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 ();
}
}
Reference: http://chunpeng.iteye.com/blog/631972