Android uses HttpClient to submit data to the server, androidhttpclient
In the previous articles, "Android uses get to submit data to the server" and "Android uses post to submit data to the server" introduces two methods for android to submit data to the server.
This article continues to introduce how to use HttpClient to submit data to the server.
HTTP protocol may be the most widely used and important protocol on the Internet. More and more Java applications need to directly access network resources through HTTP protocol. Although the basic functions for accessing HTTP are provided in the JDK java net package, for most applications, the functions provided by the JDK library itself are not rich and flexible. HttpClient is a sub-project under Apache Jakarta Common. It is used to provide an efficient, up-to-date, and function-rich client programming toolkit that supports HTTP protocol. It also supports the latest versions and suggestions of HTTP protocol. HttpClient has been applied to many projects. For example, the other two open-source projects Cactus and HTMLUnit on Apache Jakarta both use HttpClient.
The modification code is as follows:
Public void LoginHttpClientGet (View view) {String name = et_name.getText (). toString (). trim (); String pwd = et_pwd.getText (). toString (). trim (); if (TextUtils. isEmpty (name) | TextUtils. isEmpty (pwd) {Toast. makeText (this, "the user name and password cannot be blank", 0 ). show ();} else {// 1. Open a browser HttpClient client = new DefaultHttpClient (); // 2. Enter the address String path = "http: // 169.254.168.71: 8080/web/LoginServlet? Username = "+ name +" & password = "+ pwd; try {HttpGet httpGet = new HttpGet (path); // 3. Press ENTER HttpResponse response = client.exe cute (httpGet ); int code = response. getStatusLine (). getStatusCode (); if (code == 200) {InputStream is = response. getEntity (). getContent (); // convert the content of is TO ByteArrayOutputStream bos = new ByteArrayOutputStream (); byte [] buffer = new byte [1024]; int len =-1; while (len = is. Read (buffer ))! =-1) {bos. write (buffer, 0, len);} String result = new String (bos. toByteArray (); is. close (); Toast. makeText (this, result, 0 ). show ();} else {Toast. makeText (this, "request failed, cause of failure:" + code, 0 ). show () ;}} catch (Exception e) {e. printStackTrace (); Toast. makeText (this, "request failed, please check the logcat log console", 0 ). show () ;}} public void LoginHttpClientPost (View view) {String name = et_name.getText (). toString (). trim (); String pwd = et_pwd.getText (). toString (). trim (); if (TextUtils. isEmpty (name) | TextUtils. isEmpty (pwd) {Toast. makeText (this, "the user name and password cannot be blank", 0 ). show ();} else {// 1. Open a browser HttpClient client = new DefaultHttpClient (); // 2. Enter the address String path = "http: // 169.254.168.71: 8080/web/LoginServlet? Username = "+ name +" & password = "+ pwd; try {HttpPost httpPost = new HttpPost (path ); // specify the List of data entities to be submitted <NameValuePair> parameters = new ArrayList <NameValuePair> (); parameters. add (new BasicNameValuePair ("username", name); parameters. add (new BasicNameValuePair ("password", pwd); httpPost. setEntity (new UrlEncodedFormEntity (parameters, "UTF-8"); // 3. Press ENTER HttpResponse response = client.exe cute (httpPos T); int code = response. getStatusLine (). getStatusCode (); if (code == 200) {InputStream is = response. getEntity (). getContent (); // convert the content of is TO ByteArrayOutputStream bos = new ByteArrayOutputStream (); byte [] buffer = new byte [1024]; int len =-1; while (len = is. read (buffer ))! =-1) {bos. write (buffer, 0, len);} String result = new String (bos. toByteArray (); is. close (); Toast. makeText (this, result, 0 ). show ();} else {Toast. makeText (this, "request failed, cause of failure:" + code, 0 ). show () ;}} catch (Exception e) {e. printStackTrace (); Toast. makeText (this, "request failed, please check the logcat log console", 0 ). show ();}}}