Four Network Connection Methods for Android development on the seventh day

Source: Internet
Author: User

Httpurlconnection is a standard Java class.

Httpclient is a sub-project under Apache Jakarta common. It is used to provide an efficient, up-to-date, and function-rich client programming toolkit that supports http protocol. It also supports the latest versions and suggestions of HTTP protocol. Httpclient has been applied in many projects. For example, httpclient is used by two well-known open-source projects, cactus and htmlunit. For more applications that use httpclient, see container. The httpclient project is very active and many people are using it. Currently, the httpclient version is 3.0 RC4 released in 5.10.11.

Httpclient is a good open-source framework that encapsulates HTTP request headers, parameters, content bodies, responses, and so on,

Httpurlconnection is a standard Java class with nothing encapsulated. it is inconvenient to use it in Taiyuan, suchCustom re-AccessAnd some advanced functions.

1. httpurlconnection get Method

Try {URL url = new URL ("http://www.baidu.com/img/baidu_sylogo1.gif"); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setconnecttimeout (5000); Conn. setrequestmethod ("get"); // get the request response code int code = Conn. getresponsecode (); If (code = httpstatus. SC _ OK) {inputstream = Conn. getinputstream (); miv. setimagebitmap (bitmapfactory. decodestream (inputstream); toast. maketext (this, "get access successful", toast. length_short ). show () ;}} catch (exception e) {// todo auto-generated Catch Block E. printstacktrace ();}

2. httpurlconnection POST method

Try {URL url = new URL ("http://www.baidu.com/img/baidu_sylogo1.gif"); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setconnecttimeout (5000); Conn. setrequestmethod ("Post"); // get the request response code int code = Conn. getresponsecode (); If (code = httpstatus. SC _ OK) {inputstream = Conn. getinputstream (); miv. setimagebitmap (bitmapfactory. decodestream (inputstream); toast. maketext (this, "post access successful", toast. length_short ). show () ;}} catch (exception e) {// todo auto-generated Catch Block E. printstacktrace ();}

3. httpclient get Method

Try {defaulthttpclient httpclient = new defaulthttpclient (); httpparams Params = httpclient. getparams (); httpget = new httpget ("http://www.baidu.com/img/baidu_sylogo1.gif"); httpconnectionparams. setconnectiontimeout (Params, 5000); httpresponse response = httpclient.exe cute (httpget); If (response. getstatusline (). getstatuscode () = httpstatus. SC _ OK) {byte [] bytearray = entityutils. tobytearray (response. getentity (); miv. setimagebitmap (bitmapfactory. decodebytearray (bytearray, 0, bytearray. length); toast. maketext (this, "get access successful", toast. length_short ). show () ;}} catch (exception e) {e. printstacktrace ();}

4. httpclient POST method

Try {defaulthttpclient httpclient = new defaulthttpclient (); httpparams Params = httpclient. getparams (); httppost = new httppost ("http://www.baidu.com/img/baidu_sylogo1.gif"); httpconnectionparams. setconnectiontimeout (Params, 5000); httpresponse response = httpclient.exe cute (httppost); If (response. getstatusline (). getstatuscode () = httpstatus. SC _ OK) {byte [] bytearray = entityutils. tobytearray (response. getentity (); miv. setimagebitmap (bitmapfactory. decodebytearray (bytearray, 0, bytearray. length); toast. maketext (this, "post access successful", toast. length_short ). show () ;}} catch (exception e) {e. printstacktrace ();}

Mobile development QQ group: 59516399

Csdn download link: http://download.csdn.net/detail/wenwei19861106/4981424

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.