方法实在是很简单, 为你的httpclient添加一个retryhandler就ok了。代码如下:
HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException arg0, int arg1, HttpContext arg2) {
// retry a max of 5 times
if (arg1 >= 3) {
return false;
}
if (arg0 instanceof ch.boye.httpclientandroidlib.NoHttpResponseException) {
return true;
} else if (arg0 instanceof ch.boye.httpclientandroidlib.client.ClientProtocolException) {
return true;
}
return false;
}
};
sHttpClient.setHttpRequestRetryHandler(retryHandler);
如果上面的代码不好使的话,可以试着添加:
HttpProtocolParams.setUseExpectContinue(params, false);
本文介绍了一种简单的方法来实现HTTPClient的重试机制。通过添加一个自定义的HttpRequestRetryHandler,可以设置最大重试次数,并针对特定异常进行重试。此外,还提供了一个额外的配置建议。
1183

被折叠的 条评论
为什么被折叠?



