Android advanced 2: HTTP access network

Source: Internet
Author: User

Procedure:

<1>

Generate request object

Httpget = new httpget ("request address ..... ");

<2>

Generate client object

Httpclient = new defaulthttpclient ();

<3>

Execute request

Httpresponse = httpclient.exe cute (httpget );

<4>

Accept response

Httpentity = httpresponse. getentity ();

<5> get data stream

Inputstream = httpentity. getcontent ();

Note:

To add permissions: <uses-Permission Android: Name = "android. Permission. Internet"/>

Specific implementation:

Package xiaosi. httpresponse; </P> <p> Import Java. io. bufferedreader; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstream; <br/> Import Java. io. inputstreamreader; </P> <p> Import Org. apache. HTTP. httpentity; <br/> Import Org. apache. HTTP. httpresponse; <br/> Import Org. apache. HTTP. client. clientprotocolexception; <br/> Import Org. apache. HTTP. client. httpclient; <br/> Import Org. apache. HTTP. client. metho DS. httpget; <br/> Import Org. apache. HTTP. impl. client. defaulthttpclient; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. textview; </P> <p> public class httpresponseactivity extends activity <br/>{< br/> private buttonbutton = NULL; <br/> private Tex Tviewtext = NULL; <br/> private httpresponsehttpresponse = NULL; <br/> private httpentityhttpentity = NULL; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) <br/>{< br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> text = (textview) findviewbyid (R. id. text); <br/> button = (button) findviewbyid (R. id. button); <br/> button. setonclicklistener (New onclicklist Ener () {<br/> @ override <br/> Public void onclick (view v) <br/>{< br/> // generate a request object, the parameter is the address <br/> httpget = new httpget ("http://www.baidu.com"); <br/> // generate the HTTP client <br/> httpclient = new defaulthttpclient (); <br/> inputstream = NULL; <br/> // use an HTTP client to send a request object <br/> try <br/> {<br/> // send a request response <br/> httpresponse = httpclient.exe cute (httpget ); <br/> // indicates the received HTTP message. All the messages returned by the server are in httpenti Ty <br/> httpentity = httpresponse. getentity (); <br/> If (httpresponse. getstatusline (). getstatuscode () = 200) {<br/> inputstream = httpentity. getcontent (); <br/> bufferedreader reader = new bufferedreader (New inputstreamreader (inputstream); <br/> string result = ""; <br/> string line = ""; <br/> while (line = reader. readline ())! = NULL) <br/>{< br/> result = Result + line; <br/>}</P> <p> text. settext (result); <br/>}< br/> catch (clientprotocolexception e) <br/>{< br/> E. printstacktrace (); <br/>}< br/> catch (exception e) <br/>{< br/> E. printstacktrace (); <br/>}< br/> finally <br/>{< br/> try <br/>{< br/> inputstream. close (); <br/>}< br/> catch (ioexception e) <br/>{< br/> E. printstacktrace (); <br/>}< br/>}); <br/>}< 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.