Use HttpGet and HttpPost to access HTTP resources in Android

Source: Internet
Author: User

Requirements: User Login (name: username, pwd: password)

(i) HttpGet:d oget () method
Doget (): Attaches a key value pair of a parameter to the URL after it is passed

[Java]View Plaincopy
  1. public string Getresultforhttpget (String name,string pwd) throws Clientprotocolexception, ioexception{
  2. //server: Server project: servlet name
  3. String path="Http://192.168.5.21:8080/test/test";
  4. String uri=path+"? name=" +name+"&pwd=" +PWD;
  5. //name: Server-side user name, pwd: server-side password
  6. //Note that strings cannot be concatenated with spaces
  7. String result="";
  8. HttpGet httpget=new HttpGet (URI); Editor's note: Unlike HttpPost, this is where the parameters are passed in the address
  9. HttpResponse response=New Defaulthttpclient (). Execute (httpget);
  10. if (Response.getstatusline (). Getstatuscode () = = (){
  11. Httpentity entity=response.getentity ();
  12. Result=entityutils.tostring (Entity, HTTP. UTF_8);
  13. }
  14. return result;
  15. }

(ii) Httppost:dopost () method
DoPost (): Package parameters in the HTTP header pass

[Java]View Plaincopy
  1. public string Getreultforhttppost (String name,string pwd) throws Clientprotocolexception, ioexception{
  2. //server: Server project: servlet name
  3. String path="Http://192.168.5.21:8080/test/test";
  4. HttpPost httppost=New HttpPost (path);
  5. list<namevaluepair>list=new arraylist<namevaluepair> ();
  6. List.add (new Basicnamevaluepair ("name", name));
  7. List.add (new Basicnamevaluepair ("pwd", pwd));
  8. Httppost.setentity (new Urlencodedformentity (list,http. Utf_8)); //Editor's note: The difference with httpget, here is to pass the parameter with list
  9. String result="";
  10. HttpResponse response=New Defaulthttpclient (). Execute (httppost);
  11. if (Response.getstatusline (). Getstatuscode () = = (){
  12. Httpentity entity=response.getentity ();
  13. Result=entityutils.tostring (Entity, HTTP. UTF_8);
  14. }
  15. return result;
  16. }

-------------------------------------------------------------------------------------------------------

From this we know that the difference between HttpGet and HttpPost is that the former is passing the parameters in the address, the latter is passing the parameters to the list.

Use HttpGet and HttpPost to access HTTP resources in Android

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.