android okhttp 责任链简单分析
来源:互联网 发布:手机修改图片软件 编辑:程序博客网 时间:2024/06/11 09:28
前言
实践是检验真理的唯一标准。没有经过实践形成的blog都是胡扯~
看了 24中设计模式拦截器模式,看了okhttp官方的 wiki 拦截器,还是不懂怎么办!!!!!?
试试 手写一遍试试吧!手写一遍试试吧!手写一遍试试吧!
下面是仿版拦截器,主要验证一些拦截器关键点,这次我要反着来
运行结果:
我是拦截器一1我是拦截器2我是拦截器3我是最后一个拦截器 :在这里不进行向下传递,进行消耗拦截器3响应返回拦截器2响应返回拦截器一响应返回
定义拦截器 Interceptor
public interface Interceptor { Response intercept(Chain chain) throws IOException; interface Chain { Requestx request(); Response proceed(Requestx request) throws IOException; /** * Returns the connection the request will be executed on. This is only * available in the chains of network interceptors; for application * interceptors this is always null. */ @Nullable Connection connection(); }}
定义具体拦截器实现类
public class RealInterceptorChain implements Interceptor.Chain { private final List<Interceptor> interceptors; public int index; private final Requestx request; private int calls; public RealInterceptorChain(List<Interceptor> interceptors, int index, Requestx request) { this.interceptors = interceptors; this.index = index; this.request = request; } @Override public Requestx request() { return request; } @Override public Response proceed(Requestx request) throws IOException { if (index >= interceptors.size()) throw new AssertionError(); calls++; RealInterceptorChain next = new RealInterceptorChain(interceptors, index + 1, request); Interceptor interceptor = interceptors.get(index); Response response = interceptor.intercept(next); if (index + 1 < interceptors.size() && next.calls != 1) { throw new IllegalStateException("network interceptor " + interceptor + " must call proceed() exactly once"); } return response; } @Override @Nullable public Connection connection() { return new Connection(); }}
定义的Response ,RequestX,Connection
public final class Response { public Response() { super(); }}
public class Requestx { public Requestx() { }}
public class Connection {}
定义拦截器 InterceptorOne,InterceptorTwo,InterceptorThress,LastInterceptor
/** * 1.传递下去 条件 2.消耗在当前类 * * @author weichyang * */public class InterceptorOne implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { RealInterceptorChain chain2 = (RealInterceptorChain) chain; System.out.println("我是拦截器一" + chain2.index); Requestx requestx = chain.request(); Response response = chain.proceed(requestx); // 调用实现进入拦截器二 System.out.println("拦截器一响应返回"); return response; }}
public class InterceptorTwo implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { System.out.println("我是拦截器2"); Requestx requestx = chain.request(); Response response = chain.proceed(requestx); System.out.println("拦截器2响应返回"); return response; }}
public class InterceptorThree implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { System.out.println("我是拦截器3"); Requestx requestx = chain.request(); Response response = chain.proceed(requestx); System.out.println("拦截器3响应返回"); return response; }}
public class LastInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { System.out.println("我是最后一个拦截器 :在这里不进行向下传递,进行消耗"); return new Response(); }}
定义拦截器调用入口
public class Client { public static void main(String[] args) { List<Interceptor> interceptors = new ArrayList(); interceptors.add(new InterceptorOne()); interceptors.add(new InterceptorTwo()); interceptors.add(new InterceptorThree()); interceptors.add(new LastInterceptor()); Requestx request = new Requestx(); RealInterceptorChain realInterceptorChain = new RealInterceptorChain( interceptors, 0, request); try { realInterceptorChain.proceed(request); } catch (IOException e) { e.printStackTrace(); } }}
以上代码就是简化版okhttp 拦截器。
重复责任链设计模式关键点:
1.待处理问题 (request,respond)
2.责任链传递顺序 (chain.proceed(requestx))
3.边界问题,最终处理 (LastInterceptor.class 当然 前面的链式中也可以控制是否向下传递,不去调用chain.proceed(requestx))
注意: 责任设计模式顺序调用,依顺序进行处理。没的说,看着就明白,关键点在阻塞式的响应。
我是拦截器一1
我是拦截器2
我是拦截器3
我是最后一个拦截器 :在这里不进行向下传递,进行消耗
拦截器3响应返回
拦截器2响应返回
拦截器一响应返回
拦截器调用链
到这就没有了……,是不是没看懂。 回头再看一遍
阅读全文
1 0
- android okhttp 责任链简单分析
- Android中的责任链模式分析
- OkHttp之BridgeInterceptor简单分析
- Okhttp之CacheInterceptor简单分析
- OkHttp之ConnectInterceptor简单分析
- okhttp源码的简单分析
- Okhttp之CallServerInterceptor简单分析
- Android Retrofit+okHttp简单配置
- Android okHttp的简单使用
- Android开发---OkHttp简单封装
- Android OkHttp使用与分析
- okHttp 二 拦截器解析及责任链模式
- Android责任链模式
- Okhttp源码简单分析(完善ing)
- Okhttp之RealConnection建立链接简单分析
- Okhttp对http2的支持简单分析
- 《android源码分析系列》仿OkHttp-- 自己动手实现 okhttp
- Android中的责任链模式
- libreoj 101.最大流
- Analytical
- 非常可乐 HDU
- Linux下Mysql基本操作(二)
- 部署与管理ZooKeeper(转)
- android okhttp 责任链简单分析
- Canvas,Paint方法记录(一)
- 第三方登录(百度账号登录)
- hadoop mapred
- Bootstrap源码之旅-再论CSS环境搭建
- MonggoDB安装与配置力荐!!
- 贪心
- 2017/7/28 第五天
- eclipse下编译hadoop源代码(转)