duzechao / OKHttpUtils

对OkHttp进行封装,网络请求失败自动查询本地缓存,上传文件等功能
284 stars 84 forks source link

能不能给个例子,如添加拦截器什么的?写的DEMO有点简单了 #4

Closed qiuqingpo closed 8 years ago

qiuqingpo commented 8 years ago

okHttpUtils = new OKHttpUtils.Builder(this).interceptors(new AgainInterceptor()).build();//这样初始化还不对的,请多多指教

class LoggingInterceptor implements Interceptor { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request request = chain.request(); long t1 = System.nanoTime(); System.out.println(String.format("Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers())); Response response = chain.proceed(request); long t2 = System.nanoTime(); System.out.println(String.format("Received response for %s in %.1fms%n%s", response.request().url(), (t2 - t1) / 1e6d, response.headers())); return response; } } class AgainInterceptor implements Interceptor { @Override public Response intercept(Interceptor.Chain chain) throws IOException { OkHttpClient client = new OkHttpClient(); client.interceptors().add(new LoggingInterceptor()); Request request = new Request.Builder() .url("http://www.publicobject.com/helloworld.txt") .header("User-Agent", "OkHttp Example") .build(); Response response = client.newCall(request).execute(); response.body().close(); return response; } }

duzechao commented 8 years ago

interceptors(List interceptors) 这里传的是List

qiuqingpo commented 8 years ago

传完就报错哈 java.lang.UnsupportedOperationException

duzechao commented 8 years ago

List list = new ArrayList(); list.add(new AgainInterceptor()); new OKHttpUtils.Builder(this).interceptors(list).build(); 你是这样做的吗