Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to add query parameter to every request with Retrofit 2?

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); 
like image 331
Max Avatar asked Oct 05 '15 11:10

Max


People also ask

How do you append a parameter query?

Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.

Can we add query param in post request?

POST should not have query param. You can implement the service to honor the query param, but this is against REST spec.


2 Answers

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(); 
like image 199
Lukas Lechner Avatar answered Sep 20 '22 15:09

Lukas Lechner


now the Retrofit has release 2.0.0 and this is my solution:

OkHttpClient client = new OkHttpClient.Builder()         .addInterceptor(new Interceptor() {             @Override             public Response intercept(Chain chain) throws IOException {                  String uid = "0";                 long timestamp = (int) (Calendar.getInstance().getTimeInMillis() / 1000);                 String signature = MD5Util.crypt(timestamp + "" + uid + MD5_SIGN);                 String base64encode = signature + ":" + timestamp + ":" + uid;                 base64encode = Base64.encodeToString(base64encode.getBytes(), Base64.NO_WRAP | Base64.URL_SAFE);                  Request request = chain.request();                 HttpUrl url = request.url()                         .newBuilder()                         .addQueryParameter("pageSize", "2")                         .addQueryParameter("method", "getAliasList")                         .build();                  request = request                         .newBuilder()                         .addHeader("Authorization", "zui " + base64encode)                         .addHeader("from_client", "ZuiDeer")                         .url(url)                         .build();                  Response response = chain.proceed(request);                 return response;             }         }).build();   Retrofit retrofit = new Retrofit.Builder()         .baseUrl(ApiConstants.API_BASE_URL)         .client(client)         .addConverterFactory(GsonConverterFactory.create())         .addCallAdapterFactory(RxJavaCallAdapterFactory.create())         .build();  mRestfulService = retrofit.create(RestfulService.class); 
like image 24
zenghui.wang Avatar answered Sep 16 '22 15:09

zenghui.wang