I get data from REST API server
using HttpClient
.
Now I should handle when I can't get any response over 10 seconds from API Server
.
So I did it like following
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);
}
It occured like
invalid use of singleclientconnmanager: connection still allocated.
It makes sense, but I don't know how to handle.
The problem
invalid use of singleclientconnmanager: connection still allocated.
occurs since you call the execute() method twice whenever control block goes in the exception block i.e. there is any exception.
try{
// First Call
response = httpClient.execute(httpPost);
} catch(SocketTimeoutException e){
httpPost.setParams(...); // change param
//Second Call
response = httpClient.execute(httpPost);
}
Elegant way to handle this would be to use HTTPRequestRetryHandler http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d4e280