Closed qiuqingpo closed 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; } }
interceptors(List interceptors) 这里传的是List
传完就报错哈 java.lang.UnsupportedOperationException
List list = new ArrayList(); list.add(new AgainInterceptor()); new OKHttpUtils.Builder(this).interceptors(list).build(); 你是这样做的吗
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; } }