Send a POST request using the HttpClient framework in Android

Source: Internet
Author: User

/** * The way to use httpclientpost request * * @param username * @param password * @return NULL indicates a problem with the path being evaluated, text returns the requested data */public static String Httpclientpost (string username, string password) {try {//1. Open a browser httpclient client = new Defaulthttpclient ();//2 . Enter the address string path = "Http://172.22.64.156:8080/0001AndroidWebService/LoginServlet"; HttpPost post = new HttpPost ();//Specifies the data entity submitted list<namevaluepair> parameters = new arraylist<namevaluepair> (); Parameters.Add (New Basicnamevaluepair ("username", username));p arameters.add (New Basicnamevaluepair ("Password", Password));p ost.setentity (new urlencodedformentity (Parameters, "UTF-8"));//3, hit enter httpresponse response = Client.execute (post);//Gets the returned code int code = Response.getstatusline (). Getstatuscode (); if (code = = 200) {// Gets the information returned inputstream is = Response.getentity (). getcontent (); String Text = Streamutil.readstream (is); return text;} else {return null;}} catch (Exception e) {e.printstacktrace (); return null;}}

Use the HttpClient framework in Android to send a POST request

Related Article

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.