Android 网络连接-重试机制-HttpRequestRetryHandler(HttpClient)
Httpclient.setHttpRequestRetryHandler(requestRetryHandler);
/**
* 设置重连机制和异常自动恢复处理
*/
private static HttpRequestRetryHandler requestRetryHandler = new HttpRequestRetryHandler() {
// 自定义的恢复策略
public boolean retryRequest(IOException exception, int executionCount,
HttpContext context) {
// 设置恢复策略,在Http请求发生异常时候将自动重试3次
if (executionCount >= 3) {
// Do not retry if over max retry count
return false;
}
if (exception instanceof NoHttpResponseException) {
// Retry if the server dropped connection on us
return true;
}
if (exception instanceof SSLHandshakeException) {
// Do not retry on 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;
}
};
更多相关文章
- Android中的Handler消息机制
- Android IPC 机制【2】--- Messenger[1]
- Android wifi休眠策略
- 【Android策略】The content of the adapter has changed but Li
- Watchdog 工作机制解析
- Android View刷新机制
- Android Handler消息机制(源码分析)