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