okhttp3重试拦截

重试拦截器:

注意:两处while是因为如果请求中出现异常,也能进行重试,比如超时,后面会有例子。

网络拦截器,打印请求、响应时间、响应状态码,响应内容

1、带参数的get请求

测试:

String url = "http://localhost:8080/test/12346/query";Map params = new HashMap<>();params.put("aaa","111");params.put("bbb","222");HttpUtils httpUtils = HttpUtils.getInstance();httpUtils.get(url,params);打印日志如下:[main][2019-09-21 10:19:02.072] [INFO] [NetworkIntercepter.intercept:40] responseTime= 62, requestUrl= http://localhost:8080/test/12346/query?aaa=111&bbb=222, params=, responseCode= 200, result= {"result_code":"success","data":{"aaa":"111"},"path":"/test/12346/query"}

2、不带参数的get请求

测试:

String url = "http://localhost:8080/test/12346/query?ccc=1&ddd=2";HttpUtils httpUtils = HttpUtils.getInstance();httpUtils.get(url);打印日志如下:[main][2019-09-21 10:25:46.943] [INFO] [NetworkIntercepter.intercept:38] responseTime= 11, requestUrl= http://localhost:8080/test/12346/query?ccc=1&ddd=2, params=, responseCode= 200, result= {"result_code":"success","data":{"aaa":"111"},"path":"/test/12346/query"}

测试:

Gson gson = new Gson();String url = "http://localhost:8080/test/12346/query";Map params = new HashMap<>();params.put("aaa","111");params.put("bbb","222");Map bodyMap = new HashMap<>();bodyMap.put("name","zhangsan");bodyMap.put("age",15);HttpUtils httpUtils = HttpUtils.getInstance();httpUtils.post(url,params,gson.toJson(bodyMap));打印日志[main][2019-09-21 10:37:04.577] [INFO] [NetworkIntercepter.intercept:38] responseTime= 304, requestUrl= http://localhost:8080/test/12346/query?aaa=111&bbb=222, params={"name":"zhangsan","age":15}, responseCode= 200, result= {"result_code":"success","data":{"aaa":"111"},"path":"/test/12346/query"}

2、post发送json

测试:

Gson gson = new Gson();String url = "http://localhost:8080/test/12346/query";Map bodyMap = new HashMap<>();bodyMap.put("name","zhangsan");bodyMap.put("age",15);HttpUtils httpUtils = HttpUtils.getInstance();httpUtils.post(url,gson.toJson(bodyMap));

打印日志:

[main][2019-09-21 10:44:00.835] [INFO] [NetworkIntercepter.intercept:38] responseTime= 17, requestUrl= http://localhost:8080/test/12346/query, params={“name”:”zhangsan”,”age”:15}, responseCode= 200, result= {“result_code”:”success”,”data”:{“aaa”:”111″},”path”:”/test/12346/query”}

3、post发送表单

测试:

String url = "http://localhost:8080/test/12346/query";Map params = new HashMap<>();params.put("aaa","111");params.put("bbb","222");HttpUtils httpUtils = HttpUtils.getInstance();httpUtils.post(url,params);打印日志:[main][2019-09-21 10:49:54.136] [INFO] [NetworkIntercepter.intercept:38] responseTime= 21, requestUrl= http://localhost:8080/test/12346/query, params=aaa=111&bbb=222, responseCode= 200, result= {"result_code":"success","data":{"aaa":"111"},"path":"/test/12346/query"}

1、404重试

测试:

String url = "http://localhost:8080/test/12346/query2";Map params = new HashMap<>();params.put("aaa","111");params.put("bbb","222");HttpUtils httpUtils = HttpUtils.getInstance();httpUtils.post(url,params);日志打印:

[main][2019-09-21 10:56:20.495] [INFO] [NetworkIntercepter.intercept:38] responseTime= 26, requestUrl= http://localhost:8080/test/12346/query2, params=aaa=111&bbb=222, responseCode= 404, result= {“timestamp”:1569034580,”status”:404,”error”:”Not Found”,”message”:”Not Found”,”path”:”/test/12346/query2″}
[main][2019-09-21 10:56:20.506] [INFO] [NetworkIntercepter.intercept:38] responseTime= 4, requestUrl= http://localhost:8080/test/12346/query2, params=aaa=111&bbb=222, responseCode= 404, result= {“timestamp”:1569034580,”status”:404,”error”:”Not Found”,”message”:”Not Found”,”path”:”/test/12346/query2″}
[main][2019-09-21 10:56:20.512] [INFO] [NetworkIntercepter.intercept:38] responseTime= 4, requestUrl= http://localhost:8080/test/12346/query2, params=aaa=111&bbb=222, responseCode= 404, result= {“timestamp”:1569034580,”status”:404,”error”:”Not Found”,”message”:”Not Found”,”path”:”/test/12346/query2″

重试了2次,共请求3次

2、超时重试

日志打印:

[main][2019-09-21 10:59:30.086] [ERROR] [NetworkIntercepter.intercept:33] timeout
[main][2019-09-21 10:59:30.092] [INFO] [NetworkIntercepter.intercept:38] responseTime= 2009, requestUrl= http://localhost:8080/test/12346/query, params=aaa=111&bbb=222, responseCode= null, result= null
[main][2019-09-21 10:59:32.097] [ERROR] [NetworkIntercepter.intercept:33] timeout
[main][2019-09-21 10:59:32.097] [INFO] [NetworkIntercepter.intercept:38] responseTime= 2004, requestUrl= http://localhost:8080/test/12346/query, params=aaa=111&bbb=222, responseCode= null, result= null
[main][2019-09-21 10:59:34.101] [ERROR] [NetworkIntercepter.intercept:33] timeout
[main][2019-09-21 10:59:34.101] [INFO] [NetworkIntercepter.intercept:38] responseTime= 2002, requestUrl= http://localhost:8080/test/12346/query, params=aaa=111&bbb=222, responseCode= null, result= null

Original: https://www.cnblogs.com/endv/p/16300051.html
Author: Endv
Title: okhttp3重试拦截

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/551738/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球