Using the asynchronous HttpClient framework to do get,post commit data

Source: Internet
Author: User

1. Import the asynchronous HttpClient framework

: http://download.csdn.net/detail/sinat_32804317/9555641

2. Code implementation

1  Public classMainactivityextendsActivity {2 3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7     }8 9 Ten      Public voidget (View v) { OneEditText Et_name =(EditText) Findviewbyid (r.id.et_name); AEditText Et_pass =(EditText) Findviewbyid (r.id.et_pass); -          -         FinalString name =Et_name.gettext (). toString (); the         FinalString pass =Et_pass.gettext (). toString (); -String url = "Http://192.168.13.13/Web/servlet/CheckLogin?name=" + urlencoder.encode (name) + "&pass=" +Pass; -         //Creating an asynchronous HttpClient -Asynchttpclient AHC =Newasynchttpclient (); +          -         //send a GET request to submit data +Ahc.get (URL,NewMyresponsehandler ()); A     } at      -      Public voidPost (View v) { -EditText Et_name =(EditText) Findviewbyid (r.id.et_name); -EditText Et_pass =(EditText) Findviewbyid (r.id.et_pass); -          -         FinalString name =Et_name.gettext (). toString (); in         FinalString pass =Et_pass.gettext (). toString (); -String url = "Http://192.168.13.13/Web/servlet/CheckLogin"; to          +         //Creating an asynchronous HttpClient -Asynchttpclient AHC =Newasynchttpclient (); the          *         //send POST request submit data $         //encapsulate the data to be submitted to the Requestparams objectPanax NotoginsengRequestparams params =Newrequestparams (); -Params.add ("name", name); theParams.add ("Pass", pass); +Ahc.post (URL, params,NewMyresponsehandler ()); A     } the      +     classMyresponsehandlerextendsasynchttpresponsehandler{ -  $         //when the request server succeeds, this method calls the $ @Override -          Public voidOnsuccess (intStatusCode, header[] headers, -                 byte[] responsebody) { theToast.maketext (mainactivity. This,NewString (Responsebody), 0). Show (); -             Wuyi         } the  -         //request failed This method call Wu @Override -          Public voidOnFailure (intStatusCode, header[] headers, About                 byte[] responsebody, throwable error) { $Toast.maketext (mainactivity. This, "request Failed", 0). Show (); -              -         } -          A     } +      the}

Detailed Description: http://www.open-open.com/lib/view/open1352201229000.html

Using the asynchronous HttpClient framework to do get,post commit data

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.