I created a separate service generator class as shown is this guide https://futurestud.io/tutorials/retrofit-2-manage-request-headers-in-okhttp-interceptor
ApiServiceGen
I have a Creator
class like this
class Creator {
public static Services newServices() {
final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
final OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Services.HOST)
.client(client)
.addConverterFactory(GsonConverterFactory.create(GsonUtils.get()))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
return retrofit.create(Services.class);
}
}
You need to use the http client created when building the retrofit instance.
Retrofit.Builder()
.baseUrl(BASE_URL)
.client(httpClient) // This is the line
.addConverterFactory(GsonConverterFactory.create());