OkHttpClient throws exception after upgrading to OkHttp3

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() {
        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:

  • Android: 9-patch repeat pattern instead of stretching
  • How can I display a list view in an Android Alert Dialog?
  • Change the Color of ScrollView Programmatically
  • Viewflipper animation doesn't work on first swipe
  • Supporting both landscape orientations in Honeycomb
  • Transaction ID set correctly, but displayed only a submit later
  • 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?

    Related posts:

    getToken() failed. Status BAD_AUTHENTICATION error
    how to install CA certificate programmatically on Android without user interaction
    Take a photo automatically without user interaction
    Is possible set Expanded Notification as default in Big Text Notifications?
    Android: listen for Orientation change?
    android weightSum not working properly
  • Type R is already defined error
  • Error java.lang.RuntimeException: Stub! in Android with Fitnesse testing
  • How to create android spinner without down triangle on the right side of the widget
  • Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled
  • record/save audio from voice recognition intent
  • Different behavior for FillType.EVEN_ODD when adding a CornerPathEffect?
  • 2 Solutions collect form web for “OkHttpClient throws exception after upgrading to OkHttp3”

    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()
               new Interceptor() {
                 public Response intercept(Interceptor.Chain chain) throws IOException {
                       Request request = chain.request().newBuilder()
                       .addHeader("Accept", "Application/JSON").build();
                   return chain.proceed(request);
    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;
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.