Android advanced 2: HTTP connection get/POST request

Source: Internet
Author: User

 

Procedure: 1. Create an httpget (or httppost) object and pass the requested URL to the httpget (or httppost) object through the constructor; 2. Use the execute method of the defaulthttpclient class to send an http get or http post request and return the httpresponse object; 3. Return the response information through the getentity method of the httpresponse interface.

 

 

HTTP connection POST request

// Step 1: Create an httppost object <br/> httppost = new httppost (URL ); <br/> // set http post Request Parameters Using namevaluepair objects <br/> List <namevaluepair> Params = new arraylist <namevaluepair> (); <br/> Params. add (New basicnamevaluepair ("bookname", "2465158248"); <br/> system. out. println ("result1"); <br/> // set the httppost request parameter <br/> try <br/>{< br/> httppost. setentity (New urlencodedformentity (Params, HTTP. utf_8); <br/> // Step 2: Use the execute method to send an http get request and return the httpresponse object <br/> httpresponse; <br/> try <br/> {<br/> httpresponse = new defaulthttpclient(.exe cute (httppost); <br/> system. out. println ("result"); <br/> If (httpresponse. getstatusline (). getstatuscode () = 200) <br/>{< br/> // Step 3: Use the getentity method to return results. <br/> string result = entityutils. tostring (httpresponse. getentity (); <br/> system. out. println ("result" + result); <br/>}< br/> catch (clientprotocolexception E) <br/>{< br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> catch (ioexception E) <br/>{< br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> catch (unsupportedencodingexception e) <br/>{< br/> E. printstacktrace (); <br/>}< br/>}
HTTP connection GET request

String URL; <br/> // Step 1: Create an httpget object <br/> httpget = new httpget (URL); <br/> // step 2, use the execute method to send an http get request and return the httpresponse object <br/> httpresponse = new defaulthttpclient(cmd.exe cute (httpget); <br/> If (httpresponse. getstatusline (). getstatuscode () = 200) <br/>{< br/> // Step 3: Use the getentity method to return results. <br/> string result = entityutils. tostring (httpresponse. getentity (); <br/>}

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.