Android 网络连接-重试机制-HttpRequestRetryHandler(HttpClient)
2013-06-17 14:48
323 查看
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;
}
};
/**
* 设置重连机制和异常自动恢复处理
*/
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网络连接之HttpURLConnection和HttpClient
- Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点
- Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android--网络连接之HttpURLConnection和HttpClient
- android-HttpClient和HttpURLConnection判断网络连接
- Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比
- android网络连接之httpclient
- Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比
- android异步网络连接开源:Android Asynchronous Http Client
- Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比
- Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp、Volley
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient