09_android Introduction _ Use Android-async-http Open source project Get mode or POST method to achieve the landing case

Source: Internet
Author: User

based on the introduction of 08_android _android-async-http Open source project and its usage , we introduce the most common landing cases android-async-http The use of classes in open source projects. Hope to learn from you android-async-http Open source projects are helpful.

1 continue to use 03_android to implement the login interface for this case layout file with Relativelayout

2.server end of the code with the 04_android introduction _ HttpURLConnection Get way to implement the landing case of the server code snippet (5. About server I write only one servlet to handle the corresponding request processing)

3. In the application cite android-async-http open source project

Method One: find the source code of the downloaded file Library\src\main\java com package paste into the project under the src folder


Method Two: Put android-async-http-master\releases below the newest JAr package to apply to Libs


4.loginactivity Code (in the code specifically added gaze, please read carefully)

Package Com.example.lesson03;import Org.apache.http.header;import Com.loopj.android.http.asynchttpclient;import Com.loopj.android.http.asynchttpresponsehandler;import Com.loopj.android.http.requestparams;import Android.app.activity;import Android.os.bundle;import Android.text.textutils;import Android.view.View;import Android.widget.edittext;import Android.widget.textview;import Android.widget.toast;public class LoginActivity Extends Activity {//DECLARE control private EditText et_name, et_pass;private TextView tv_result; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//Get Control object et _name = (EditText) Findviewbyid (r.id.et_name); et_pass = (EditText) Findviewbyid (r.id.et_pass); tv_result = (TextView) Findviewbyid (R.id.tv_result);}  /** * Click the event triggered by the button control * @param v */public void login (View v) {//Get control's Idint id = v.getid ();//The corresponding processing by ID inference switch (ID) {case r.id.btn_login://gets the text content of the control string userName = Et_name.gettext (). TostriNg ();//Usernamestring Userpass = Et_pass.gettext (). toString ();//user password//infers if username and password are empty if ( Textutils.isempty (Username.trim ()) | | Textutils.isempty (Userpass.trim ())) {Toast.maketext (this, "username or password cannot be empty", Toast.length_long). Show (); else {//Send request to server//call: Loginbyasynchttpclientpost (UserName, Userpass); Loginbyasynchttpclientget (UserName, Userpass);} Break;}} /** * Implemented with Asynchttpclient POST method * @param userName * @param userpass */public void Loginbyasynchttpclientpost (String use Rname, String userpass) {asynchttpclient client = new Asynchttpclient ();//Create an asynchronous request for the clients object String url = "http://172.16.237.20 0:8080/video/login.do "; Define the requested address//create request for the encapsulated object Requestparams params = new Requestparams ();p arams.put ("username", username); Set the requested parameter name and the number of Params.put ("Userpass", userpass);//Set the requested name and reference//Run Post method client.post (URL, params, new  Asynchttpresponsehandler () {/** * method successfully processed * StatusCode: The status code of the response; headers: The corresponding header information, for example, response time, response of the server; * Responsebody: bytes of response content */@Overridepublic void onSuccess (int statusCode, header[] headers,byte[] responsebody) {if (StatusCode = =) {Tv_result.settext (New String ( Responsebody)); Set the displayed text}}/** * Method of failure Handling * ERROR: Response failure message encapsulated in this exception object */@Overridepublic void onfailure (int statusCode, header[] Headers,b Yte[] responsebody, throwable error) {error.printstacktrace ();//The wrong print information out of the path}}); /** * Implemented with Asynchttpclient get method * @param userName * @param userpass */public void Loginbyasynchttpclientget (String UserN Ame, String userpass) {//Create asynchronous Client Object asynchttpclient = new Asynchttpclient ();//request address String URL = "http://172.16.237 .200:8080/video/login.do ";//create the encapsulated object of the request parameter requestparams params = new Requestparams ();p arams.put (" username ", username) ; Set the requested parameter name and the number of Params.put ("Userpass", userpass);//Set the requested name and the number of parameters//When sending a GET request, the URL address corresponding to the parameter, the anonymous callback object Client.get (URL, params, New Asynchttpresponsehandler () {@Overridepublic void onsuccess (int statusCode, header[] headers,byte[] responsebody) {/ /Successful processing Method System.out.println ("StatusCode-------------------" + STATUSCODE);//Traverse header information for (int i = 0; i < headers.length; i++) {Header header = Headers[i]; System.out.println ("header------------Name:" + header.getname () + ",--Value:" + header.getvalue ());} Sets the control content Tv_result.settext (new String (Responsebody));} @Overridepublic void onfailure (int statusCode, header[] headers,byte[] responsebody, throwable error) {// Method of failure handling Error.printstacktrace ();}});}}

5. Program execution results such as:


09_android Introduction _ Use Android-async-http Open source project Get mode or POST method to achieve the landing case

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.