Search code examples
javaandroidretrofitandroid-networkingokhttp

Is there a replacement for the RequestInterceptor in Retrofit 2?


I ran into a problem that RequestInterceptor has been removed from the Retrofit 2. Earlier, my RestAdapter builder was as follows:

private RestAdapter.Builder getBuilder(RequestInterceptor requestInterceptor) {
    RestAdapter.Builder builder = new RestAdapter.Builder()
            .setEndpoint(BuildConfig.SERVER_URL)
            .setClient(connectionClient)
            .setRequestInterceptor(requestInterceptor)
            .setConverter(new JacksonConverter());
    return builder;
}

As far as I know, at the moment it's recommended to use interceptor from the OkHttp library instead of the RequestInterceptor.

I couldn't find the exemplary implementation of this approach, therefore, be grateful for any help in this matter.

Thanks!


Solution

  • intercepts have to be set through OkHttp in retrofit 2

    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.interceptors().add(...)
    

    and then register it to Retrofit

    Retrofit restAdapter = new Retrofit.Builder()
            .baseUrl(Constants.BASE_URL)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build();