I need to add a query parameter to every request made by Retrofit 2.0.0-beta2 library. I found this solution for Retrofit 1.9, but how to add RequestInterceptor
in newest library version?
My interface:
@GET("user/{id}")
Call<User> getUser(@Path("id")long id);
@GET("users/")
Call<List<User>> getUser();
Client:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(CLIENT) // custom OkHTTP Client
.build();
service = retrofit.create(userService.class);
For the sake of completeness, here is the full code you need to add a parameter to every Retrofit 2.x request using a OkHttp-Interceptor:
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
HttpUrl url = request.url().newBuilder().addQueryParameter("name","value").build();
request = request.newBuilder().url(url).build();
return chain.proceed(request);
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("...")
.client(client)
.build();