2016-07-13 12 views
21

Próbuję włączyć rejestrowanie z modernizacją, ale otrzymuję ten wyjątek:Modernizacja rejestrowanie przechwytujących wyjątek

07-13 12:44:53.278 28698-29248/com.xxxx.debug E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher 
                         Process: com.xxxx.debug, PID: 28698 
                         java.lang.NoSuchMethodError: No virtual method log(Ljava/lang/String;)V in class Lokhttp3/internal/Platform; or its super classes (declaration of 'okhttp3.internal.Platform' appears in /data/data/com.xxxx.debug/files/instant-run/dex/slice-realm-optional-api_16b022358933b490d810e358ea76b13cd4d88163-classes.dex) 
                          at okhttp3.logging.HttpLoggingInterceptor$Logger$1.log(HttpLoggingInterceptor.java:109) 
                          at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:157) 
                          at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190) 
                          at com.xxxx.api.RetrofitClient$1.intercept(RetrofitClient.java:59) 
                          at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190) 
                          at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163) 
                          at okhttp3.RealCall.access$100(RealCall.java:30) 
                          at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127) 
                          at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
                          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                          at java.lang.Thread.run(Thread.java:818) 

I to jest jak ja to robię:

public class RetrofitClient { 

    private static MyService instance; 

    public static MyService getInstance(Context context) { 
     if (instance == null) { 
      instance = newInstance(context); 
     } 
     return instance; 
    } 

    private static MyService newInstance(Context context) { 
     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl(context.getString(R.string.base_url)) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .client(getClient()) 
       .build(); 

     return retrofit.create(MyService.class); 
    } 

    @NonNull 
    private static OkHttpClient getClient() { 
     HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor() 
       .setLevel(HttpLoggingInterceptor.Level.BODY); 

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

         Request request = original.newBuilder() 
           .header("api-key", "...") 
           .header("version-app", "-") 
           .header("platform", "android") 
           .header("version", "-") 
           .header("device", "-") 
           .method(original.method(), original.body()) 
           .build(); 

         Response response = chain.proceed(request);// <-- CRASH 

         return response; 
        } 
       }) 
       .addInterceptor(interceptor); 

     return httpClient.build(); 
    } 
} 

Próbowałem dodając tylko jeden przechwytujący, ale nadal się zawiesza. Używam tych zależności:

compile 'com.squareup.retrofit2:retrofit:2.1.0' 
compile 'com.squareup.retrofit2:converter-gson:2.1.0' 
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' 

Co ja tu robię źle?

EDIT: To jest zawstydzające ... problem rozwiązany. Nic nie zmieniłem, poprawiam wszystkie moje zatwierdzenia i nie zmieniłem ani jednego, ani drugiego. Problem nie był więc związany z zależnościami lub wersjami gradu.

Na szczęście ktoś rzuci trochę światła na ten wyjątek!

Odpowiedz

6

można spróbować dodać poniższą zależność

compile 'com.squareup.okhttp3:okhttp:3.4.1' 

i daj mi znać, jeśli istnieje jakikolwiek postęp

Można również odnieść to link.

38

2.1.0 Modernizacja polega na OkHttp 3.3.0, więc chciałbym używać tej samej wersji dla Interceptor Logging (który jest częścią OkHttp):

compile 'com.squareup.okhttp3:logging-interceptor:3.3.0' 
1

W moim przypadku ta para z zależnościami rozwiązany problem:

compile 'com.squareup.retrofit2:retrofit:2.1.0' 
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1' 

Zresztą problem jest w konsystencji zależności ...