我想在我的Android应用程序中实现HttpRequestRetryHandler。我已阅读HttpRequestRetryHandler here的文档。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要求?
你能解释一下问题3的答案多一点,然后我们应该如何重试包含HttpPost的实体? –
@codingcrow如您所知,'GET'主要用于检索数据(只读类),因此它是幂等的。在这种情况下重试将是直截了当的。另一方面,'POST'可以有任何类型的操作,例如更新,插入,删除,电子邮件等。在这种情况下,重试不是那么简单。在尝试重试之前,您需要了解当前状态。 –
对不起。 “当前状态”的含义是什么?我有一个JSONArray作为ByteArrayEntity到HttpPost中的服务器?有关如何在异常情况下重试的任何提示。 –