我正在尝试在我的 Android 应用程序中实现 HttpRequestRetryHandler。我在这里阅读了 HttpRequestRetryHandler 的文档。
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(
IOException exception,
int executionCount,
HttpContext context) {
if (executionCount >= 5) {
// Do not retry if over max retry count
return false;
}
if (exception instanceof InterruptedIOException) {
// Timeout
return false;
}
if (exception instanceof UnknownHostException) {
// Unknown host
return false;
}
if (exception instanceof ConnectException) {
// Connection refused
return false;
}
if (exception instanceof SSLException) {
// SSL handshake exception
return false;
}
HttpRequest request = (HttpRequest) context.getAttribute(
ExecutionContext.HTTP_REQUEST);
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
// Retry if the request is considered idempotent
return true;
}
return false;
}
};
httpclient.setHttpRequestRetryHandler(myRetryHandler);
文档中的示例指出不应重试某些异常。例如,不应重试 InterruptedIOException。
问题1为什么不应重试InterruptedIOException?
问题 2如何知道哪些异常要重试,哪些不应该重试?例如 - 我们是否应该重试 ConnectionTimeoutException 和 SocketTimeoutException?
文档还说HttpClient 假定 GET 和 HEAD 等非实体封闭方法是幂等的,而 POST 和 PUT 等实体封闭方法则不是。
问题 3这是否意味着我们不应该重试 POST 和 PUT 方法,如果不是,那么我们应该如何重试 HttpPost 请求?