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