1
votes

Starting with OkHttp v3 OkHttpClient is immutable and so is the interceptor's list.

Starting with Retrofit 2.0-beta3 it depends on OkHttp v3.

In my app I need to parse Google Analytics Campain refferer key, which has "a=1&b=2&c=3" format, and append it to sign up request URL.

With previous version of retrofit it was easy to do by adding

retrofit.client().interceptors().add(0, chain -> {
    final Request httpRequest = chain.request();
    return chain.proceed(httpRequest.newBuilder().url(httpRequest.httpUrl().toString() + "&" + request.getUtmParams()).build());
});

before executing call and

retrofit.client().interceptors().remove(0);

after call is proceed.

Can't figure out how to achieve the same behaviour with the new version. Please help.

UPD 1. The goal is to control request URL of single request. Appending string to the end is just an example. Using @QueryMap is an option, but it requires parsing parameter string which I try to avoid.

3
OkHttp Interceptors didn't much changed. Parsing utm from url is easy with uri utils. - Nikola Despotoski

3 Answers

0
votes

Use query map.

public interface ApiService {
@GET("YOUR_BASE_URL")
Call<Login> authenticate(@Query("a") String a, @Query("b") String b,@Query("c") String c);
}
0
votes

You can install the interceptor once and then use a header to trigger the interceptor. See this post for details.

-1
votes

Depending on your request Method but here I am using GET

// You can also use a Map Query to acheive the same result

public interface ApiService {

    @GET("api/{a}/{b}/{c}")
    Call<Login> authenticate(@Path("a") String a, @Path("b") String password, @Path("c") String c);
}

Create a EntityObject

public EntityObject {

   Public String a;

   Public String b;

   Public String c;
}

Use the retrofit to make your request with your parameters

private void makeRemoteRequest(String a, String b, String c){

  Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

 ApiInterface mService = retrofit.create(ApiService.class);

Call<Login> mService = mService.authenticate(a, b, c);
    mService.enqueue(new Callback<Login>() {
        @Override
        public void onResponse(Call<Login> call, Response<Login> response) {


        }

        @Override
        public void onFailure(Call<Login> call, Throwable t) {

        }
    });
}