HttpClient Implementing a POST request

Source: Internet
Author: User

String Httpurl ="http://192.168.1.110:8080/httpget.jsp"; /** Namevaluepair represents a header,list<namevaluepair> store all header fields * urlencodedformentity similar to urlencoder statements for URL encoding * H Ttppost a POST request similar to HTTP * Client.execute () is similar to making a request and returning response*/    //HttpPost Connection ObjectHttpPost HttpRequest =NewHttpPost (Httpurl); //use Namevaluepair to save the post parameters to be passedList<namevaluepair>params=NewArraylist<namevaluepair>(); //to add a parameter to passNamevaluepair Pair1=NewBasicnamevaluepair ("par","Httpclient_android_post")); params. Add (PAIR1); //Params.add (New Basicnamevaluepair ("par", "httpclient_android_post"));  Try   {   //Set character setsHttpentity httpentity =NewUrlencodedformentity (params,"gb2312"); //Request HttpRequesthttprequest.setentity (httpentity); //get the default httpclientHttpClient HttpClient =Newdefaulthttpclient (); //Get HttpResponseHttpResponse HttpResponse =Httpclient.execute (HttpRequest); //HTTPSTATUS.SC_OK indicates successful connection    if(Httpresponse.getstatusline (). Getstatuscode () = =HTTPSTATUS.SC_OK) {     //gets the returned stringString strresult =entityutils.tostring (Httpresponse.getentity ());    Mtextview.settext (strresult); }   Else{Mtextview.settext ("Request Error!"); }  } Catch(clientprotocolexception e) {mtextview.settext (E.getmessage (). toString ()); } Catch(IOException e) {mtextview.settext (E.getmessage (). toString ()); } Catch(Exception e) {mtextview.settext (E.getmessage (). toString ());}

HttpClient Implementing a POST request

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.