Android Learning Essay 12 (Network: Using the asynchronous HttpClient framework)

Source: Internet
Author: User

Send a GET, POST request using the asynchronous HttpClient framework

Search asyn-http https://github.com/search?utf8=?&q=asyn-http Download in https://github.com/loopj/android-async-http

 Public classMainactivityextendsActivity {Handler Handler=NewHandler () { Public voidhandlemessage (android.os.Message msg) {Toast.maketext (mainactivity. This, (String) msg.obj, 0). Show ();    }    }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidClick1 (View v) {//get user-entered account passwordEditText Et_name =(EditText) Findviewbyid (r.id.et_name); EditText Et_pass=(EditText) Findviewbyid (R.id.et_pass); String name=Et_name.gettext (). toString (); String Pass=Et_pass.gettext (). toString (); String Path= "Http://169.254.244.136/Web2/servlet/Login"; //send a GET request using asynchronous HttpClientAsynchttpclient client =Newasynchttpclient (); //encapsulate the data to be submitted in the RP objectRequestparams RP =NewRequestparams (); Rp.put ("Name", name); Rp.put ("Pass", pass); //send a GET requestClient.get (Path, RP,NewMyHandler ()); }        classMyHandlerextendsasynchttpresponsehandler{//callback when the request succeeds, the corresponding code is@Override Public voidOnsuccess (intStatusCode, header[] headers,byte[] responsebody) {Toast.maketext (mainactivity). This,NewString (Responsebody), 0). Show (); }        //callback when request fails, the corresponding code is not@Override Public voidOnFailure (intStatusCode, header[] headers,byte[] responsebody, throwable error) {Toast.maketext (mainactivity). This, "request Failed", 0). Show (); }            }             Public voidClick2 (View v) {//get user-entered account passwordEditText Et_name =(EditText) Findviewbyid (r.id.et_name); EditText Et_pass=(EditText) Findviewbyid (R.id.et_pass); String name=Et_name.gettext (). toString (); String Pass=Et_pass.gettext (). toString (); String Path= "Http://169.254.244.136/Web2/servlet/Login"; //sending a POST request using asynchronous HttpClientAsynchttpclient client =Newasynchttpclient (); //encapsulate the data to be submitted in the RP objectRequestparams RP =NewRequestparams (); Rp.put ("Name", name); Rp.put ("Pass", pass); //send a GET requestClient.post (Path, RP,NewMyHandler ()); }    }

Android Learning Essay 12 (Network: Using the asynchronous HttpClient framework)

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.