For a cleaner code approach have a separate class for the interceptor chain like this:
public class LanguageInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
HttpUrl url = chain.request()
.url()
.newBuilder()
.addQueryParameter("name","value")
.build();
Request request = chain.request()
.newBuilder()
.url(url)
.build();
Response response = chain.proceed(request);
return response;
}
}
Then in the class where you defined your retrofit instance you add the instance object to it with .addInterceptor(new LanguageInterceptor())
like this:
public static Retrofit getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.addInterceptor(new LanguageInterceptor())
.addInterceptor(interceptor)
.build();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
Log.e(TAG, "getClient: base url " + retrofit.baseUrl());
}
return retrofit;
}