Apache HttpClient HttpRequestRetryHandler never invoked
I'm using the Apache HTTP commons DefaultHttpClient
and after construction, I'm setting its retry handler:
httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(final IOException ioe,
final int numRetry, final HttpContext context)
{
Log.d(TAG, "retry handler received exception of type: " + ioe.getClass().getName() + ", num retries: " + numRetry);
if (numRetry > 4) { // 3 retries
return false;
}
// Some exceptions we can retry without knowledge of which methods are being invoked
if (ioe instanceof NoHttpResponseException
|| ioe instanceof UnknownHostException
|| ioe instanceof SocketException) {
}
return false;
}
});
The server that I am sending my requests to frequently times out, and in the catch
of my execute() call, I receive an IO error, "The operation timed out" however I never see any the "retry h开发者_C百科andler received exception of type" log statement in my console output. All of my requests are POST requests, but they are idempotent--they're safe to call multiple times without adverse side-effects.
Am I setting up the retry handler incorrectly? Is the retry handler only invoked in certain scenarios?
Return true if you handled an exceptional case in your handler.
That should keep the exception from beeing thrown.
Also you could handle the Timeout exception in your handler as well.
httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(final IOException ioe,
final int numRetry, final HttpContext context)
{
Log.d(TAG, "retry handler received exception of type: " + ioe.getClass().getName() + ", num retries: " + numRetry);
if (numRetry > 4) { // 3 retries
return false;
}
// Some exceptions we can retry without knowledge of which methods are being invoked
if (ioe instanceof NoHttpResponseException
|| ioe instanceof UnknownHostException
|| ioe instanceof SocketException
// Replace with the actual type of the exception
|| ioe instanceof TimeoutException) {
return true;
}
return false;
}
});
Try to use DefaultHttpRequestRetryHandler which is a subclass of HttpRequestRetryHandler
精彩评论