android okhttp 責(zé)任鏈簡單分析

運(yùn)行結(jié)果:

我是攔截器一1
我是攔截器2
我是攔截器3
我是最后一個(gè)攔截器 :在這里不進(jìn)行向下傳遞,進(jìn)行消耗
攔截器3響應(yīng)返回
攔截器2響應(yīng)返回
攔截器一響應(yīng)返回

定義攔截器 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();
    }
}

定義具體攔截器實(shí)現(xiàn)類

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.消耗在當(dāng)前類
 * 
 * @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); // 調(diào)用實(shí)現(xiàn)進(jìn)入攔截器二

        System.out.println("攔截器一響應(yīng)返回");

        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響應(yīng)返回");

        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響應(yīng)返回");
        return response;
    }

}
public class LastInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {

        System.out.println("我是最后一個(gè)攔截器 :在這里不進(jìn)行向下傳遞,進(jìn)行消耗");

        return new Response();
    }

}

定義攔截器調(diào)用入口

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();
        }

    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容