"Turn" Pro Android Learning Note (73): HTTP Service (7): Androidhttpclient

Source: Internet
Author: User

The article reproduced only for non-commercial nature, and can not be accompanied by virtual currency, points, registration and other conditions, reprint must indicate the source: http://blog.csdn.net/flowingflying/

I wonder if this article is the shortest one in the series. We note that the HttpClient class comes from Apache Org.apache.http.client.HttpClient, and a detailed tutorial can be viewed http://hc.apache.org/ httpcomponents-client-ga/tutorial/html/.

Android2.2 introduced HttpClient's inheritance class androidhttpclient, and made some default settings for HttpClient, making it easy for developers to develop Android apps such as connection timeouts and socket timeouts set to 20 seconds, Connection manager set to threads Afeclientconnmanager. In most cases, it can be replaced directly with HttpClient. But there are some places to be aware of.

Creating an Androidhttpclient instance

Use the static function newinstance () to create the Androidhttpclient instance. From the Connection Manager setting to Threadsafeclientconnmanager, you can guess that the Androidhttpclient object is provided to the entire application, so you can understand why the static function is used to create it. Examples are as follows:

Androidhttpclient httpClient = androidhttpclient.newinstance("my-http-agent-string");

The parameter that is taken is the HTTP request message header user-agent. For example, in the last study we showed the contents of the User-agent message header by grabbing a packet:

dalvik/1.6.0 (Linux; u; Android 4.2.2; SDK build/jb_mr1.1)

Once we have created the Androidhttpclient instance, it is not possible to change the parameter settings inside, which means that androidhttpclient helps the developer set these parameters and does not allow changes. If we must change, we can set it in the HttpGet object.

Execute () cannot execute in the UI thread

Execute () cannot be executed in the UI linearity, which is the main thread, otherwise it will be abnormal and must be performed in a background process.

Close () and compression processing

When we are done, close () should be executed to free up memory. Androidhttpclient provides some static methods for handling compression responses, including modifyrequesttoacceptgzipresponse (HttpRequest request), getcompressedentity ( byte[] data, Contentresolver resolver), getungzippedcontent (httpentity entity).

RELATED Links: My Android development related articles

"Turn" Pro Android Learning Note (73): HTTP Service (7): Androidhttpclient

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.