Unsupported operation: Android, Retrofit, OkHttp. Adding interceptor in OkHttpClient

This issue occurs when you change Retrofit 2.0-beta2 to Retrofit 2.0-beta3. You have to use builder if you want to create OkHttpClient object.

Change :

 OkHttpClient okClient = new OkHttpClient();

 okClient.interceptors().add(new Interceptor() {
       @Override
       public Response intercept(Interceptor.Chain chain) throws IOException {
            Request original = chain.request();

            // Request customization: add request headers
            Request.Builder requestBuilder = original.newBuilder()
                    .header("Authorization", token)
                    .method(original.method(), original.body());

            Request request = requestBuilder.build();
            return chain.proceed(request);
        }
 });

to :

 OkHttpClient okClient = new OkHttpClient.Builder()
           .addInterceptor(
               new Interceptor() {
                 @Override
                 public Response intercept(Interceptor.Chain chain) throws IOException {
                       Request original = chain.request();

                       // Request customization: add request headers
                       Request.Builder requestBuilder = original.newBuilder()
                               .header("Authorization", token)
                               .method(original.method(), original.body());

                       Request request = requestBuilder.build();
                       return chain.proceed(request);
                   }
               })
           .build();

It should resolve your problem.


Try this if the other answer doesn't work:

OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .addInterceptor(new MyInterceptor())
    .build();
retrofit = new Retrofit.Builder()
    .baseUrl("http://google.com")
    .addConverterFactory(GsonConverterFactory.create())
    .client(okHttpClient)
    .build();