本文主要应用了OkHttp的Interceptor来实现自定义重试次数

虽然OkHttp自带retryOnConnectionFailure(true)方法可以实现重试,但是不支持自定义重试次数,所以有时并不能满足我们的需求。

#1.自定义重试拦截器:

/**
* 重试拦截器
*/
public class RetryIntercepter implements Interceptor { public int maxRetry;//最大重试次数
private int retryNum = ;//假如设置为3次重试的话,则最大可能请求4次(默认1次+3次重试) public RetryIntercepter(int maxRetry) {
this.maxRetry = maxRetry;
} @Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
System.out.println("retryNum=" + retryNum);
Response response = chain.proceed(request);
while (!response.isSuccessful() && retryNum < maxRetry) {
retryNum++;
System.out.println("retryNum=" + retryNum);
response = chain.proceed(request);
}
return response;
}
}

#2.测试场景类:

 public class RetryTest {
String mUrl = "https://www.baidu.com/";
OkHttpClient mClient; @Before
public void setUp() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY); mClient = new OkHttpClient.Builder()
.addInterceptor(new RetryIntercepter())//重试
.addInterceptor(logging)//网络日志
.addInterceptor(new TestInterceptor())//模拟网络请求
.build();
} @Test
public void testRequest() throws IOException {
Request request = new Request.Builder()
.url(mUrl)
.build();
Response response = mClient.newCall(request).execute();
System.out.println("onResponse:" + response.body().string());
} class TestInterceptor implements Interceptor { @Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String url = request.url().toString();
System.out.println("url=" + url);
Response response = null;
if (url.equals(mUrl)) {
String responseString = "{\"message\":\"我是模拟的数据\"}";//模拟的错误的返回值
response = new Response.Builder()
.code()
.request(request)
.protocol(Protocol.HTTP_1_0)
.body(ResponseBody.create(MediaType.parse("application/json"), responseString.getBytes()))
.addHeader("content-type", "application/json")
.build();
} else {
response = chain.proceed(request);
}
return response;
}
} }

#3.输出结果:

 retryNum=
--> GET https://www.baidu.com/ HTTP/1.1
--> END GET
url=https://www.baidu.com/
<-- null https://www.baidu.com/ (13ms)
content-type: application/json {"message":"我是模拟的数据"}
<-- END HTTP (-byte body)
retryNum=
--> GET https://www.baidu.com/ HTTP/1.1
--> END GET
url=https://www.baidu.com/
<-- null https://www.baidu.com/ (0ms)
content-type: application/json {"message":"我是模拟的数据"}
<-- END HTTP (-byte body)
retryNum=
--> GET https://www.baidu.com/ HTTP/1.1
--> END GET
url=https://www.baidu.com/
<-- null https://www.baidu.com/ (0ms)
content-type: application/json {"message":"我是模拟的数据"}
<-- END HTTP (-byte body)
retryNum=
--> GET https://www.baidu.com/ HTTP/1.1
--> END GET
url=https://www.baidu.com/
<-- null https://www.baidu.com/ (0ms)
content-type: application/json {"message":"我是模拟的数据"}
<-- END HTTP (-byte body)
onResponse:{"message":"我是模拟的数据"}

#4.结果分析:
>1. 这里我用一个TestInterceptor拦截器拦截掉真实的网络请求,实现response.code的自定义
2. 在RetryIntercepter中,通过response.isSuccessful()来对响应码进行判断,循环调用了多次chain.proceed(request)来实现重试拦截
3. 从输出中可以看到,一共请求了4次(默认1次+重试3次)。

#5.其它实现方式
如果你是使用OkHttp+Retrofit+RxJava,你也可以使用retryWhen操作符:retryWhen(new RetryWithDelay())来实现重试机制

 public class RetryWithDelay implements Func1<Observable<? extends Throwable>, Observable<?>> {

         private final int maxRetries;
private final int retryDelayMillis;
private int retryCount; public RetryWithDelay(int maxRetries, int retryDelayMillis) {
this.maxRetries = maxRetries;
this.retryDelayMillis = retryDelayMillis;
} @Override
public Observable<?> call(Observable<? extends Throwable> attempts) {
return attempts
.flatMap(new Func1<Throwable, Observable<?>>() {
@Override
public Observable<?> call(Throwable throwable) {
if (++retryCount <= maxRetries) {
// When this Observable calls onNext, the original Observable will be retried (i.e. re-subscribed).
LogUtil.print("get error, it will try after " + retryDelayMillis + " millisecond, retry count " + retryCount);
return Observable.timer(retryDelayMillis,
TimeUnit.MILLISECONDS);
}
// Max retries hit. Just pass the error along.
return Observable.error(throwable);
}
});
}
}

OkHttp自定义重试次数的更多相关文章

  1. Dubbo重试次数

    服务超时后重试次数[retries],不包含第一次调用,0代表不重试 *我们应该在幂等方法上设置重试次数[查询.删除.修改],在非幂等方法上禁止设置重试次数. ★幂等:指多次运行方法所产生的最终效果是 ...

  2. 一个带重试次数的curl 函数

    <?php/** * [curl 带重试次数] * @param [type] $url [访问的url] * @param [type] $post [$POST参数] * @param in ...

  3. SpringCloud Feign 之 超时重试次数探究

    SpringCloud Feign 之 超时重试次数探究 上篇文章,我们对Feign的fallback有一个初步的体验,在这里我们回顾一下,Fallback主要是用来解决依赖的服务不可用或者调用服务失 ...

  4. Redis解决“重试次数”场景的实现思路

    很多地方都要用到重试次数限制,不然就会被暴力破解.比如登录密码. 下面不是完整代码,只是伪代码,提供一个思路. 第一种(先声明,这样写有个bug) import java.text.MessageFo ...

  5. python重试次数装饰器

    目录 重试次数装饰器 重试次数装饰器 前言, 最近在使用tornado框架写Restful API时遇到很多的问题. 有框架的问题, 有异步的问题. 虽然tornado 被公认为当前python语言最 ...

  6. Shiro密码重试次数限制

    如在 1 个小时内密码最多重试 5 次,如果尝试次数超过 5 次就锁定 1 小时,1 小时后可再次重试,如果还是重试失败,可以锁定如 1 天,以此类推,防止密码被暴力破解.我们通过继承 HashedC ...

  7. 使用Python请求http/https时设置失败重试次数

    设置请求时的重试规则 import requests from requests.adapters import HTTPAdapter s = requests.Session() a = HTTP ...

  8. python requests 配置超时及重试次数

    import requests from requests.adapters import HTTPAdapter s = requests.Session() s.mount('http://', ...

  9. springcloud超时时间与重试次数配置

    #hystrix配置hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=120000ribbon.Conn ...

随机推荐

  1. Linux之 网卡发包、接包 error 、droped 情况

    1. 查看各个网卡发送.接受包情况oracle@hbdw2:/oratmp2$netstat -niKernel Interface tableIface MTU Met RX-OK RX-ERR R ...

  2. 【转载】MySQL · 性能优化· InnoDB buffer pool flush策略漫谈

    背景 我们知道InnoDB使用buffer pool来缓存从磁盘读取到内存的数据页.buffer pool通常由数个内存块加上一组控制结构体对象组成.内存块的个数取决于buffer pool inst ...

  3. 探讨 java中 接口和对象的关系

    接口是对象么?接口可以有对象么?这个问题要跟类比对着,或许更好理解;类是对象的模版.接口不是类,所以:接口肯定不是对象的模版.那接口跟对象有什么样的关系?还是得从类入手;因为类实现了接口,所以可以说, ...

  4. 【JavaScript数组】

    1.什么是数组--Array 数组就是一组数据的集合 其表现形式就是内存中的一段连续的内存地址 数组名称其实就是连续内存地址的首地址 2.关于js中的数组特点 数组定义时无需指定数据类型 数组定义时可 ...

  5. Object.prototype.toString.call()方法浅谈

    使用Object.prototype上的原生toString()方法判断数据类型,使用方法如下: Object.prototype.toString.call(value) 1.判断基本类型: Obj ...

  6. 更换HomeBrew源

    比较少用brew,只有之前安装Opencv的时候用过一次,后面有人问我怎么装,于是帮他研究了一下.MacOS的brew其实就是通过两个git仓库(brew和homebrew-core)来实现的源更新机 ...

  7. 51 nod 1203 JZPLCM

    原题链接 长度为N的正整数序列S,有Q次询问,每次询问一段区间内所有数的lcm(即最小公倍数).由于答案可能很大,输出答案Mod 10^9 + 7.   例如:2 3 4 5,询问[1,3]区间的最小 ...

  8. BZOJ 1029: [JSOI2007]建筑抢修【优先队列+贪心策略】

    1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec  Memory Limit: 162 MBSubmit: 4810  Solved: 2160[Submit][Statu ...

  9. BZOJ 2219: 数论之神

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2219 N次剩余+CRT... 就是各种奇怪的分类讨论.. #include<cstrin ...

  10. HDU--2021

    发工资咯:) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...