Android HttpClient Settings timeout

Source: Internet
Author: User
Android HttpClient Settings timeout

3.X is like this.

HttpClient httpclient=new defaulthttpclient ();
4.3 Is this.
Closeablehttpclient httpclient = Httpclients.createdefault ();
Of course, the above changes are just a few small changes, everyone look at the API everyone will be.
I want to talk about is the time-out setting, HttpClient has three kinds of timeout settings, recently busy, no time to specific summary, later on, I will talk about some of the simplest and most easy-to-use method of time-out.

This is a 3. Timeout setting method for X


HttpClient client = new HttpClient ();
Client.setconnectiontimeout (30000);
Client.settimeout (30000);
HttpClient httpclient= new HttpClient ();
Httpclient.gethttpconnectionmanager (). Getparams (). Setconnectiontimeout (5000);

Timeout setting for 4.X version (obsolete after 4.3)

HttpClient httpclient=new defaulthttpclient ();
Httpclient.getparams (). Setparameter (coreconnectionpnames.connection_timeout,2000);//Connection time
Httpclient.getparams (). Setparameter (coreconnectionpnames.so_timeout,2000);//Data transfer time

4.3 Version timeout setting

Closeablehttpclient httpclient = Httpclients.createdefault ();
HttpGet httpget=new httpget ("http://www.baidu.com");//http Get request (post similarity)
Requestconfig requestconfig = Requestconfig.custom (). SetSocketTimeout. Setconnecttimeout (). build ();//Set Request and Transfer timeout
Httpget.setconfig (Requestconfig);
Httpclient.execute (HttpGet),//Execute request
btw,4.3 version does not set timeout, once the server is not responding, wait time n long (>24 hours)

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.