private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Response originalResponse = chain.proceed(chain.request()); return originalResponse.newBuilder() .header("Cache-Control", String.format("max-age=%d, only-if-cached, max-stale=%d", 120, 0)) .build(); } };
OkHttpClient defaultHttpClient = new OkHttpClient.Builder() .addInterceptor( new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request request = chain.request().newBuilder() .addHeader("Accept", "Application/JSON").build(); return chain.proceed(request); } }).build();
private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Response originalResponse = chain.proceed(chain.request()); return originalResponse.newBuilder() .removeHeader("Pragma") .header("Cache-Control", String.format("max-age=%d", 60)) .build(); } };
OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request newRequest = chain.request().newBuilder() .addHeader("X-TOKEN", "VAL") .build(); return chain.proceed(newRequest); } }) .build(); Picasso picasso = new Picasso.Builder(context) .downloader(new OkHttp3Downloader(client)) .build();
OkHttpClient okClient = new OkHttpClient.Builder() .addInterceptor( new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request original = chain.request(); // Request customization: add request headers Request.Builder requestBuilder = original.newBuilder() .header("Authorization", token) .method(original.method(), original.body()); Request request = requestBuilder.build(); return chain.proceed(request); } }) .build();
public WebServiceClient() { OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(10, TimeUnit.SECONDS); client.setReadTimeout(30, TimeUnit.SECONDS); client.interceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { return onOnIntercept(chain); } }); Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .client(client) .build(); webService = retrofit.create(WebService.class); }
OkHttpClient httpClient = new OkHttpClient.Builder() .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Builder ongoing = chain.request().newBuilder(); ongoing.addHeader("Accept", "application/json;versions=1"); if (isUserLoggedIn()) { ongoing.addHeader("Authorization", getToken()); } return chain.proceed(ongoing.build()); } }) .build(); Retrofit retrofit = new Retrofit.Builder() // ... extra config .client(httpClient) .build();
.addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException {
private Interceptor interceptor = new Interceptor() { @Override public okhttp3.Response intercept(Chain chain) throws IOException { okhttp3.Response response = chain.proceed(chain.request()); locationHistory.add(response.header("Location")); return response; } };
OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(20, TimeUnit.SECONDS) .readTimeout(20, TimeUnit.SECONDS) .addInterceptor(new Interceptor()) .build();
OkHttpClient client = new OkHttpClient().newBuilder() .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { final Request original = chain.request(); final Request authorized = original.newBuilder() .addHeader("Cookie", "cookie-name=cookie-value") .build(); return chain.proceed(authorized); } }) .build();
Interceptor interceptor = new Interceptor() { @Override public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException { okhttp3.Request.Builder ongoing = chain.request().newBuilder(); ongoing.addHeader("Content-Type", "application/x-www-form-urlencoded"); ongoing.addHeader("Accept", "application/json"); return chain.proceed(ongoing.build()); } };
private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Response originalResponse = chain.proceed(chain.request()); return originalResponse.newBuilder() .header("Cache-Control", "max-age=900") .build(); } };
private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Response originalResponse = chain.proceed(chain.request()); return originalResponse.newBuilder() .header("Cache-Control", "max-age=60") .build(); } };
/** Dangerous interceptor that rewrites the server's cache-control header. */ private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Response originalResponse = chain.proceed(chain.request()); return originalResponse.newBuilder() .header("Cache-Control", "max-age=60") .build(); } };
OkHttpClient client = new OkHttpClient.Builder() .addNetworkInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Response originalResponse = chain.proceed(chain.request()); return originalResponse.newBuilder() .body(new ProgressResponseBody(originalResponse.body(), progressListener)) .build(); } }) .build();
OkHttpClient httpClient = new OkHttpClient.Builder() .addNetworkInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { System.out.println("url: " + chain.request().url()); return chain.proceed(chain.request()); } }) .build(); System.out.println(httpClient.newCall(new Request.Builder().url("http://google.com").build()).execute());
OkHttpClient client = new OkHttpClient(); client.interceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { ... } }); Retrofit retrofit = new Retrofit.Builder() .baseUrl("...") .client(client) .build();
public class InterceptorTest { void myTest() { OkHttpClient client = new OkHttpClient(); client.interceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Response response = chain.proceed(chain.request()); // Do anything with response here return response; } }); } }
final String myToken = "Zm9vOmJhcg=="; Interceptor interceptor = new Interceptor() { @Override public okhttp3.Response intercept(Chain chain) throws IOException { Request newRequest = chain.request().newBuilder() .addHeader("Authorization", "Basic " + yourtoken).build(); return chain.proceed(newRequest); } }; OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.interceptors().add(interceptor); OkHttpClient client = builder.build();