OkHttp中的retryOnConnectionFailure(true)方法可以实现错误重试,但不支持自定义重试次数。
1.通过自定义一个Interceptor实现错误重试次数。
1.1自定义RetryInterceptor
public class RetryInterceptor implements Interceptor
{
public int executionCount; // 最大重试次数
private long retryInterval; // 重试的间隔
RetryInterceptor(Builder builder)
{
this.executionCount = builder.executionCount;
this.retryInterval = builder.retryInterval;
}
@Override
public Response intercept(Chain chain) throws IOException
{
Request request = chain.request();
Response response = chain.proceed(request);
int retryNum = 0;
while ((response == null || !response.isSuccessful()) && retryNum <= executionCount)
{
final long nextInterval = getRetryInterval();
try
{
Thread.sleep(nextInterval);
}
catch (

本文介绍了如何在OkHttp中通过自定义Interceptor实现错误重试次数的控制,详细讲解了RetryInterceptor的创建和使用方法。同时,也探讨了利用RxJava来实现错误重试的功能,包括自定义RetryException及其调用方式。
最低0.47元/天 解锁文章
1万+

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



