Retrofit with OkHttp interceptor

suggest change

This example shows how to use a request interceptor with OkHttp. This has numerous use cases such as:

Retrofit.Builder builder = new Retrofit.Builder()

.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .baseUrl(“https://api.github.com/”);

if (!TextUtils.isEmpty(githubToken)) {

// githubToken: Access token for GitHub OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Request newReq = request.newBuilder() .addHeader(“Authorization”, format(“token %s”, githubToken)) .build(); return chain.proceed(newReq); } }).build();

builder.client(client);

}

return builder.build().create(GithubApi.class);

See OkHttp topic for more details.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents