/** * 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