android httpclient 設定逾時

來源:互聯網
上載者:User
android httpclient 設定逾時

3.X是這樣的

HttpClient httpClient=new DefaultHttpClient();
4.3是這樣的
CloseableHttpClient httpClient = HttpClients.createDefault();
當然,上面這些變化只不過是一些小變化,大家看看API大家就都會了。
我要講的是逾時設定,HttpClient有三種逾時設定,最近比較忙,沒時間具體歸納總結,以後再補上,我這裡就講一些最簡單最易用的逾時設定方法。

這是個3.X的逾時設定方法


HttpClient client = new HttpClient();
client.setConnectionTimeout(30000); 
client.setTimeout(30000);
HttpClient httpClient= new HttpClient(); 
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

4.X版本的逾時設定(4.3後已淘汰)

HttpClient httpClient=new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,2000);//連線時間
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,2000);//資料轉送時間

4.3版本逾時設定

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet=new HttpGet("http://www.baidu.com");//HTTP Get請求(POST雷同)
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();//佈建要求和傳輸逾時時間
httpGet.setConfig(requestConfig);
httpClient.execute(httpGet);//執行請求
BTW,4.3版本不設定逾時的話,一旦伺服器沒有響應,等待時間N久(>24小時)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.