Android Siege Lion httpclient post/get Pass Parameters

Source: Internet
Author: User

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

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.