GET mode
First, the parameter is put into list, and then the parameter is URL encoded list<basicnamevaluepair> params = new linkedlist<basicnamevaluepair> (); Params.add (New Basicnamevaluepair ("param1", "China"));p Arams.add (New Basicnamevaluepair ("param2", "value2"));// Encode the parameter string param = Urlencodedutils.format (params, "UTF-8");//baseurlstring baseUrl = "http://ubs.free4lab.com/php/ Method.php ";//url and parameter stitching httpget GetMethod = new HttpGet (BaseUrl +"? "+ param); HttpClient HttpClient = new defaulthttpclient (); try { HttpResponse response = Httpclient.execute (GetMethod);// Initiate 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 mode
In the same way as get, first put the parameter in listparams = new linkedlist<basicnamevaluepair> ();p Arams.add (New Basicnamevaluepair ("param1 "," POST Method "));p Arams.add (New Basicnamevaluepair (" Param2 "," second Parameter ")), try { HttpPost postmethod = new HttpPost ( BASEURL); Postmethod.setentity (New urlencodedformentity (params, "utf-8")); Fill in the parameters into post entity 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 httpclient Basic Use method