当前位置: 首页 > news >正文

上海做网站比较有名的公司郑州 互联网 公司网站

上海做网站比较有名的公司,郑州 互联网 公司网站,如何建立一个学校网站,沈阳公司做网站的本文主要应用了OkHttp的Interceptor来实现自定义重试次数 虽然OkHttp自带retryOnConnectionFailure(true)方法可以实现重试#xff0c;但是不支持自定义重试次数#xff0c;所以有时并不能满足我们的需求。 #1.自定义重试拦截器#xff1a; /*** 重试拦截器*/ public clas…本文主要应用了OkHttp的Interceptor来实现自定义重试次数 虽然OkHttp自带retryOnConnectionFailure(true)方法可以实现重试但是不支持自定义重试次数所以有时并不能满足我们的需求。 #1.自定义重试拦截器 /*** 重试拦截器*/ public class RetryIntercepter implements Interceptor {public int maxRetry;//最大重试次数private int retryNum 0;//假如设置为3次重试的话则最大可能请求4次默认1次 3次重试public RetryIntercepter(int maxRetry) {this.maxRetry maxRetry;}Overridepublic 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.测试场景类 1 public class RetryTest {2 String mUrl https://www.baidu.com/;3 OkHttpClient mClient;4 5 Before6 public void setUp() {7 HttpLoggingInterceptor logging new HttpLoggingInterceptor();8 logging.setLevel(HttpLoggingInterceptor.Level.BODY);9 10 mClient new OkHttpClient.Builder() 11 .addInterceptor(new RetryIntercepter(3))//重试 12 .addInterceptor(logging)//网络日志 13 .addInterceptor(new TestInterceptor())//模拟网络请求 14 .build(); 15 } 16 17 Test 18 public void testRequest() throws IOException { 19 Request request new Request.Builder() 20 .url(mUrl) 21 .build(); 22 Response response mClient.newCall(request).execute(); 23 System.out.println(onResponse: response.body().string()); 24 } 25 26 class TestInterceptor implements Interceptor { 27 28 Override 29 public Response intercept(Chain chain) throws IOException { 30 Request request chain.request(); 31 String url request.url().toString(); 32 System.out.println(url url); 33 Response response null; 34 if (url.equals(mUrl)) { 35 String responseString {\message\:\我是模拟的数据\};//模拟的错误的返回值 36 response new Response.Builder() 37 .code(400) 38 .request(request) 39 .protocol(Protocol.HTTP_1_0) 40 .body(ResponseBody.create(MediaType.parse(application/json), responseString.getBytes())) 41 .addHeader(content-type, application/json) 42 .build(); 43 } else { 44 response chain.proceed(request); 45 } 46 return response; 47 } 48 } 49 50 } #3.输出结果 1 retryNum02 -- GET https://www.baidu.com/ HTTP/1.13 -- END GET4 urlhttps://www.baidu.com/5 -- 400 null https://www.baidu.com/ (13ms)6 content-type: application/json7 8 {message:我是模拟的数据}9 -- END HTTP (35-byte body) 10 retryNum1 11 -- GET https://www.baidu.com/ HTTP/1.1 12 -- END GET 13 urlhttps://www.baidu.com/ 14 -- 400 null https://www.baidu.com/ (0ms) 15 content-type: application/json 16 17 {message:我是模拟的数据} 18 -- END HTTP (35-byte body) 19 retryNum2 20 -- GET https://www.baidu.com/ HTTP/1.1 21 -- END GET 22 urlhttps://www.baidu.com/ 23 -- 400 null https://www.baidu.com/ (0ms) 24 content-type: application/json 25 26 {message:我是模拟的数据} 27 -- END HTTP (35-byte body) 28 retryNum3 29 -- GET https://www.baidu.com/ HTTP/1.1 30 -- END GET 31 urlhttps://www.baidu.com/ 32 -- 400 null https://www.baidu.com/ (0ms) 33 content-type: application/json 34 35 {message:我是模拟的数据} 36 -- END HTTP (35-byte body) 37 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())来实现重试机制 1 public class RetryWithDelay implements Func1Observable? extends Throwable, Observable? {2 3 private final int maxRetries;4 private final int retryDelayMillis;5 private int retryCount;6 7 public RetryWithDelay(int maxRetries, int retryDelayMillis) {8 this.maxRetries maxRetries;9 this.retryDelayMillis retryDelayMillis; 10 } 11 12 Override 13 public Observable? call(Observable? extends Throwable attempts) { 14 return attempts 15 .flatMap(new Func1Throwable, Observable?() { 16 Override 17 public Observable? call(Throwable throwable) { 18 if ( retryCount maxRetries) { 19 // When this Observable calls onNext, the original Observable will be retried (i.e. re-subscribed). 20 LogUtil.print(get error, it will try after retryDelayMillis millisecond, retry count retryCount); 21 return Observable.timer(retryDelayMillis, 22 TimeUnit.MILLISECONDS); 23 } 24 // Max retries hit. Just pass the error along. 25 return Observable.error(throwable); 26 } 27 }); 28 } 29 } 更多专业前端知识请上 【猿2048】www.mk2048.com
http://wiki.neutronadmin.com/news/381646/

相关文章:

  • 网站 备案简单代码大全
  • 湘乡网站建设举报网站建设自查报告
  • 网站建设注意要求广西区建设厅网站
  • 学做网站论坛注册公司实缴和认缴有什么区别
  • 泗阳做网站做cpa搭建哪个网站比较好
  • 网站推广营销运营方式手机可以看的网站
  • app营销网站建设wordpress 兼容性
  • 关于网站备案的44个问题seo整站优化报价
  • 建购物网站的详细步骤徐州建站模板公司
  • 如何建设好企业的网站维护WordPress导航条之间得跳转
  • 新手学做网站 iso ed2kapp宣传推广方案
  • 环保空调东莞网站建设惠州seo排名外包
  • 漳州电脑网站建设自己用钢管做里闪弹枪视频和照网站
  • 北京建设工程联合验收网站网站备案 影响
  • wordpress 搬站googlechrome
  • 微信公众号网站自己做导航条让建站公司做网站需要什么
  • 淄博教育学校网站建设网站设计模板简约
  • 做网站推广的学习软件
  • 正能量不良网站推荐2020建设事业单位网站多少钱
  • 网站建设合同违约责任网站开发相关技术
  • 重庆哪里可以做公司网站中国科技成就的例子
  • 如何做好品牌网站建设策划书开发一个简单的小程序
  • 郫县城乡规划建设管理局网站万网站长工具
  • 果洛电子商务网站建设互联网公司排名2019
  • 做网站不需要编程的软件python和c++学哪个好
  • dw和sql做网站新手创业开什么店最好
  • 西安网站制作平台网站设计公司产品介绍
  • 哪家微网站做的好广州网站设计培训
  • 个人网站建设第一步微信app制作
  • 电子商务网站建设的体会常州seo