34

I'm using following lines of code to add a default header to all of my requests sent using Retrofit2:

private static OkHttpClient defaultHttpClient = new OkHttpClient();
static {
    defaultHttpClient.networkInterceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request().newBuilder()
                    .addHeader("Accept", "Application/JSON").build();
            return chain.proceed(request);
        }
    });
}

After upgrading retrofit to beta-3 version, I had to upgrade OkHttp to OkHttp3 also (actually I just changed package names from okhttp to okhttp3, the library is included inside retrofit). After that I get exceptions from this line:

defaultHttpClient.networkInterceptors().add(new Interceptor());

Caused by: java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Collections.java:932)


Caused by: java.lang.ExceptionInInitializerError


What is the problem here?

Ashkan Sarlak
  • 7,124
  • 6
  • 39
  • 51

2 Answers2

74

You have to use builder if you want to create OkHttp(3)Client object.

Try change this:

private static OkHttpClient defaultHttpClient = new OkHttpClient();

To something like this:

  OkHttpClient defaultHttpClient = new OkHttpClient.Builder()
       .addInterceptor(
           new Interceptor() {
             @Override
             public Response intercept(Interceptor.Chain chain) throws IOException {
                   Request request = chain.request().newBuilder()
                   .addHeader("Accept", "Application/JSON").build();
               return chain.proceed(request);
              }
           }).build();
V.Sch
  • 1,078
  • 8
  • 11
  • Yes. it's working fine. thanks for perfect solution. – Android Help Jul 14 '16 at 19:54
  • Hello, implementing above code says,... `incompatible types: required : retrofit2.Response <> ; Found: okHttp3.Response` ... ???? – Bhuro Sep 08 '16 at 15:10
  • this is how is working in my case `OkHttpClient defaultHttpClient = new OkHttpClient.Builder() .addInterceptor( new Interceptor() { @Override public okhttp3.Response intercept(Chain chain) throws IOException { Request request = chain.request().newBuilder() .addHeader("Accept", "Application/JSON") .addHeader("Cookie", s) .addHeader("Cache-Control", "max-age=25000").build(); return chain.proceed(request); } }).build();` ... any idea why? Sorry, but I am new to Retrofit lib ... – Bhuro Sep 08 '16 at 15:13
1
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile "com.squareup.retrofit2:converter-gson:2.1.0"
compile "com.squareup.retrofit2:adapter-rxjava:2.1.0"
compile 'com.squareup.okhttp3:logging-interceptor:3.4.0'

You should probably use these versions. Just put them, sync your gradle, delete all imports, and try again.

import okhttp3.Interceptor;

import okhttp3.OkHttpClient;

import okhttp3.Request;

import okhttp3.Response;

import okhttp3.logging.HttpLoggingInterceptor;

import retrofit2.Retrofit;

import retrofit2.converter.gson.GsonConverterFactory;
Ahlem Jarrar
  • 1,129
  • 1
  • 12
  • 33