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