1 Public classRegistactivityextendsActivity {2 3 PrivateEditText name, age;4 Privatebutton button;5 6 @Override7 protected voidonCreate (Bundle savedinstancestate) {8 //TODO auto-generated Method Stub9 Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.register); OneName =(EditText) Findviewbyid (r.id.edittext2name); AAge =(EditText) Findviewbyid (r.id.edittext1age); -button =(Button) Findviewbyid (r.id.button1); - theButton.setonclicklistener (NewOnclicklistener () { - - @Override - Public voidOnClick (View v) { + //TODO auto-generated Method Stub -String url= "Http://172.16.166.195:8080/web/Myservlet"; + //new HttpThread1 (URL, Name.gettext (). ToString (), Age.gettext (). ToString ()). Start (); Aurl = url + "? Name=" +name.gettext (). toString () + "&age=" +Age.gettext (). toString (); at NewHttpclientthread (URL). Start ();//tune Dohttpclientget (); - NewHttpclientthread (Url,name.gettext (). ToString (), Age.gettext (). ToString ()). Start ();//tune Dohttpclientpost (); - } - }); - } - in}
1 Public classHttpclientthreadextendsThread {2 PrivateString URL;3 PrivateString name;4 PrivateString age;5 6 Publichttpclientthread (String url) {7 //TODO auto-generated Constructor stub8 This. url =URL;9 }Ten One Publichttpclientthread (string url, string name, String age) { A //TODO auto-generated Constructor stub - This. url =URL; - This. Name =name; the This. Age =Age ; - } - - Private voidDohttpclientpost () { +HttpClient client =Newdefaulthttpclient (); -HttpPost post =Newhttppost (URL); + //to store data through Namevaluepair Aarraylist<namevaluepair> list =NewArraylist<namevaluepair>(); atList.add (NewBasicnamevaluepair ("name", name)); -List.add (NewBasicnamevaluepair ("Age", age)); - - Try { - //set the data to be sent -Post.setentity (Newurlencodedformentity (list)); inHttpResponse response =Client.execute (POST); - to if(Response.getstatusline (). Getstatuscode () = =HTTPSTATUS.SC_OK) { +String content =entityutils.tostring (Response.getentity ()); - } the}Catch(unsupportedencodingexception e) { * //TODO auto-generated Catch block $ e.printstacktrace ();Panax Notoginseng}Catch(clientprotocolexception e) { - //TODO auto-generated Catch block the e.printstacktrace (); +}Catch(IOException e) { A //TODO auto-generated Catch block the e.printstacktrace (); + } - $ } $ - Private voidDohttpclientget () { - //Create a HttpGet object theHttpGet HttpGet =Newhttpget (URL); - //Create a HttpClient objectWuyiHttpClient client =Newdefaulthttpclient (); the - HttpResponse response; Wu - Try{//Send Request AboutResponse =Client.execute (httpget); $ //Judging Type - if(Response.getstatusline (). Getstatuscode () = =HTTPSTATUS.SC_OK) { - //remove the data returned by the server -String content =entityutils.tostring (Response.getentity ()); A +SYSTEM.OUT.PRINTLN ("--->content" +content); the - } $}Catch(clientprotocolexception e) { the //TODO auto-generated Catch block the e.printstacktrace (); the}Catch(IOException e) { the //TODO auto-generated Catch block - e.printstacktrace (); in } the the } About the @Override the Public voidrun () { the //TODO auto-generated Method Stub + //dohttpclientget (); - dohttpclientpost (); the }Bayi}
Android Siege Lion httpclient post/get pass parameters