|
GET Request Server
First put the parameters in the list, and then URL code the parameters
list<basicnamevaluepair> params = new linkedlist<basicnamevaluepair> ();
Params.add (New Basicnamevaluepair ("param1", "China"));
Params.add (New Basicnamevaluepair ("param2", "value2"));
Encode the parameter
String param = Urlencodedutils.format (params, "UTF-8");
BaseUrl
String baseUrl = "http://ubs.free4lab.com/php/method.php";
Stitching URLs and parameters
HttpGet GetMethod = new HttpGet (BaseUrl + "?" + param);
HttpClient HttpClient = new Defaulthttpclient ();
try {
HttpResponse response = Httpclient.execute (GetMethod); Initiating a GET request
LOG.I (TAG, "Rescode =" + Response.getstatusline (). Getstatuscode ()); Get the response code
LOG.I (TAG, "result =" + entityutils.tostring (response.getentity (), "Utf-8"));//Get Server response content
} catch (Clientprotocolexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
|
|
|
Post method
As with the Get method, first put the parameter in the list
params = new linkedlist<basicnamevaluepair> ();
Params.add (New Basicnamevaluepair ("param1", "POST Method"));
Params.add (New Basicnamevaluepair ("Param2", "second parameter"));
try {
HttpPost Postmethod = new HttpPost (BASEURL);
Postmethod.setentity (New urlencodedformentity (params, "utf-8")); Fill in the post entity with the parameters
HttpResponse response = Httpclient.execute (Postmethod); Execute POST method
LOG.I (TAG, "Rescode =" + Response.getstatusline (). Getstatuscode ()); Get the response code
LOG.I (TAG, "result =" + entityutils.tostring (response.getentity (), "Utf-8")); Get response Content
} catch (Unsupportedencodingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Clientprotocolexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
|
|
Android Server interactive get and post