当前位置: 首页 > 工具软件 > squareup > 使用案例 >

com.squareup.okhttp.Interceptor

屠和洽
2023-12-01

retrift 集成了okhttp,所以,我们以后就不用再单独的引用http的jar 了。
但是,今天遇到一个问题,就是okhttp是这样设置一些intercept的:

private static OkHttpClient client = new OkHttpClient.Builder()
            .addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
            .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)//不添加离线缓存无效
            .cache(cache)
            .build();

在com.squareup.okhttp.Interceptor 里面一直弄不出来,
我也开始.,只能.出来一个list,我草,我没有思想准备就是list之后可以.add();

private boolean createHttpClient() {
    httpClient = new OkHttpClient();
    httpClient.setConnectTimeout(10, TimeUnit.SECONDS);
    httpClient.setReadTimeout(10, TimeUnit.SECONDS);
    httpClient.networkInterceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            Request requestWithUserAgent = originalRequest.newBuilder()
                    .removeHeader("User-Agent")
                    .addHeader("User-Agent", userAgent)
                    .build();
            return chain.proceed(requestWithUserAgent);
        }
    });

    return true;
}

转载于:https://www.cnblogs.com/caoxinyu/p/10568745.html

 类似资料:

相关阅读

相关文章

相关问答