OkHttpClient 是否有最大重试次数
Does OkHttpClient have a max retry count
我正在为 OkHttpClient 设置连接失败时重试选项。
client = new OkHttpClient();
client.setRetryOnConnectionFailure(true);
我想知道它会继续尝试多少次。查看 source code 我没有看到任何最大限制。如何配置客户端在尝试几次后停止尝试?
没有内置方法来设置最大限制,但您可以添加如下拦截器。
client.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
// try the request
Response response = chain.proceed(request);
int tryCount = 0;
int maxLimit = 3; //Set your max limit here
while (!response.isSuccessful() && tryCount < maxLimit) {
Log.d("intercept", "Request failed - " + tryCount);
tryCount++;
// retry the request
response = chain.proceed(request);
}
// otherwise just pass the original response on
return response;
}
});
可以找到有关拦截器的更多详细信息here。
Configure this client to retry or not when a connectivity problem is
encountered. By default, this client silently recovers from the
following problems:
- Unreachable IP addresses. If the URL's host has multiple IP addresses, failure to reach any individual IP address doesn't fail the overall request. This can increase availability of multi-homed services.
Stale pooled connections. The ConnectionPool reuses sockets to decrease request latency, but these connections will occasionally time out.
Unreachable proxy servers. A ProxySelector can be used to attempt multiple proxy servers in sequence, eventually falling back to a direct connection.
Set this to false to avoid retrying requests when doing so is destructive. In this case the calling application should do its own recovery of connectivity failures.
但一般来说,我认为它的目的是在存在现有的失效连接或可以重试的备用路径时重试。不要无限期地重试完全相同的事情。
另见 ConnectionSpecSelector.connectionFailed
我在下面做了一个解决方法:
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
// try the request
Response response = doRequest(chain,request);
int tryCount = 0;
while (response == null && tryCount <= RetryCount) {
String url = request.url().toString();
url = switchServer(url);
Request newRequest = request.newBuilder().url(url).build();
tryCount++;
// retry the request
response = doRequest(chain,newRequest);
}
if(response == null){//important ,should throw an exception here
throw new IOException();
}
return response;
}
private Response doRequest(Chain chain,Request request){
Response response = null;
try{
response = chain.proceed(request);
}catch (Exception e){
}
return response;
}
OkHttp 可能会“积极地”在 slow/unreliable 连接上重复您的请求,直到成功。这是针对 GET、POST 或任何其他类型的请求
完成的
根据RetryAndFollowUpInterceptor
的源代码是20
/**
* How many redirects and auth challenges should we attempt? Chrome follows 21 redirects; Firefox,
* curl, and wget follow 20; Safari follows 16; and HTTP/1.0 recommends 5.
*/
private static final int MAX_FOLLOW_UPS = 20;
我正在为 OkHttpClient 设置连接失败时重试选项。
client = new OkHttpClient();
client.setRetryOnConnectionFailure(true);
我想知道它会继续尝试多少次。查看 source code 我没有看到任何最大限制。如何配置客户端在尝试几次后停止尝试?
没有内置方法来设置最大限制,但您可以添加如下拦截器。
client.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
// try the request
Response response = chain.proceed(request);
int tryCount = 0;
int maxLimit = 3; //Set your max limit here
while (!response.isSuccessful() && tryCount < maxLimit) {
Log.d("intercept", "Request failed - " + tryCount);
tryCount++;
// retry the request
response = chain.proceed(request);
}
// otherwise just pass the original response on
return response;
}
});
可以找到有关拦截器的更多详细信息here。
Configure this client to retry or not when a connectivity problem is encountered. By default, this client silently recovers from the following problems:
- Unreachable IP addresses. If the URL's host has multiple IP addresses, failure to reach any individual IP address doesn't fail the overall request. This can increase availability of multi-homed services.
Stale pooled connections. The ConnectionPool reuses sockets to decrease request latency, but these connections will occasionally time out.
Unreachable proxy servers. A ProxySelector can be used to attempt multiple proxy servers in sequence, eventually falling back to a direct connection.
Set this to false to avoid retrying requests when doing so is destructive. In this case the calling application should do its own recovery of connectivity failures.
但一般来说,我认为它的目的是在存在现有的失效连接或可以重试的备用路径时重试。不要无限期地重试完全相同的事情。
另见 ConnectionSpecSelector.connectionFailed
我在下面做了一个解决方法:
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
// try the request
Response response = doRequest(chain,request);
int tryCount = 0;
while (response == null && tryCount <= RetryCount) {
String url = request.url().toString();
url = switchServer(url);
Request newRequest = request.newBuilder().url(url).build();
tryCount++;
// retry the request
response = doRequest(chain,newRequest);
}
if(response == null){//important ,should throw an exception here
throw new IOException();
}
return response;
}
private Response doRequest(Chain chain,Request request){
Response response = null;
try{
response = chain.proceed(request);
}catch (Exception e){
}
return response;
}
OkHttp 可能会“积极地”在 slow/unreliable 连接上重复您的请求,直到成功。这是针对 GET、POST 或任何其他类型的请求
完成的根据RetryAndFollowUpInterceptor
的源代码是20
/**
* How many redirects and auth challenges should we attempt? Chrome follows 21 redirects; Firefox,
* curl, and wget follow 20; Safari follows 16; and HTTP/1.0 recommends 5.
*/
private static final int MAX_FOLLOW_UPS = 20;