Essential Android Retrofit2 edit forum

Retrofit with OkHttp interceptor

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.


Table Of Contents
37 Retrofit2
39 ACRA
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close