Android uses httpclient to submit data to the server

Source: Internet
Author: User

In the previous articles, "Android uses get to submit data to the server" "Android uses post to submit data to the server" describes two ways to submit data from Android to the server

This article continues to introduce data submission to the server using HttpClient

The HTTP protocol is probably the most widely used and most important protocol on the Internet now, and more and more Java applications need to access network resources directly through the HTTP protocol. While the basic functionality of accessing the HTTP protocol has been provided in the Java NET package of the JDK, the JDK library itself provides a lack of functionality and flexibility for most applications. HttpClient is a sub-project under Apache Jakarta Common to provide an efficient, up-to-date, feature-rich client programming toolkit that supports the HTTP protocol, and it supports the latest versions and recommendations of the HTTP protocol. HttpClient has been used in many projects, such as the two other open source projects that are famous on Apache Jakarta Cactus and Htmlunit use HttpClient.

Modify the code as follows:

     Public voidloginhttpclientget (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, "User name password cannot be empty", 0). Show (); } Else {            //1. Open a browser         HttpClient client = new  defaulthttpclient (); //2. Enter the addressString Path = "Http://169.254.168.71:8080/web/LoginServlet?username=" + name + "&password=" +pwd; Try{ httpget httpget = new  httpget (path); //3. Hit Enter             HttpResponse response = Client.execute (httpget);                 int code = response.getstatusline (). Getstatuscode (); if(Code = = 200) {InputStream is=response.getentity (). getcontent (); //Convert the content of is to a stringBytearrayoutputstream BOS =NewBytearrayoutputstream (); byte[] buffer =New byte[1024]; intLen =-1;  while(len = is.read (buffer))! =-1) {bos.write (buffer,0, Len); The String result=NewString (Bos.tobytearray ());                    Is.close (); Toast.maketext ( This, result, 0). Show (); } Else{Toast.maketext ( This, "request failed, reason for failure:" + code, 0). Show (); }            } Catch(Exception e) {e.printstacktrace (); Toast.maketext ( This, "The request failed, please check the Logcat log console", 0). Show (); }        }    }         Public voidloginhttpclientpost (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, "User name password cannot be empty", 0). Show (); } Else {            //1. Open a browser          HttpClient client = new  defaulthttpclient (); //2. Enter the addressString Path = "Http://169.254.168.71:8080/web/LoginServlet?username=" + name + "&password=" +pwd; Try{ HttpPost httppost = new  httppost (path); //specify the data entity to commit to              list<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. Hit Enter               HttpResponse response = Client.execute (httppost); intCode =response.getstatusline (). Getstatuscode (); if(Code = = 200) {InputStream is=response.getentity (). getcontent (); //Convert the content of is to a stringBytearrayoutputstream BOS =NewBytearrayoutputstream (); byte[] buffer =New byte[1024]; intLen =-1;  while(len = is.read (buffer))! =-1) {bos.write (buffer,0, Len); The String result=NewString (Bos.tobytearray ());                    Is.close (); Toast.maketext ( This, result, 0). Show (); } Else{Toast.maketext ( This, "request failed, reason for failure:" + code, 0). Show (); }            } Catch(Exception e) {e.printstacktrace (); Toast.maketext ( This, "The request failed, please check the Logcat log console", 0). Show (); }        }    }

Android uses httpclient to submit data to the server

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.