HttpClient SocketTimeoutException 处理

HttpClient SocketTimeoutException handling

我使用 HttpClientREST API server 获取数据。
现在我应该处理当我无法从 API Server.
获得超过 10 秒的任何响应时 所以我喜欢关注

HttpClient httpClient = new DefaultHttpClient();

HttpParams httpParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);

HttpPost httpPost = new HttpPost("api server address");
httpPost.setParams(...);

HttpResponse response;
try{
    response = httpClient.execute(httpPost);    
} catch(SocketTimeoutException e){
    httpPost.setParams(...); // change param
    response = httpClient.execute(httpPost);
}

它发生了
invalid use of singleclientconnmanager: connection still allocated.
有道理,就是不知道怎么处理。

问题

invalid use of singleclientconnmanager: connection still allocated.

发生是因为每当控制块进入异常块时调用 execute() 方法两次,即有任何异常。

try{
    // First Call
    response = httpClient.execute(httpPost);    
} catch(SocketTimeoutException e){
    httpPost.setParams(...); // change param
    //Second Call
    response = httpClient.execute(httpPost);
}

处理这个问题的优雅方法是使用 HTTPRequestRetryHandler http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d4e280