Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit logging interceptor exception

Tags:

I am trying to enable Logging with Retrofit but I am getting this exception:

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) 

And this is how I am doing it:

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();     } } 

I have tried adding only one interceptor but still crashing. I am using this dependencies:

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' 

What am I doing wrong here?

EDIT: Well this is embarrassing... the problem solved itself. I changed nothing, I am revising all my commits and both RetrofitClient and build.gradle haven't changed. The problem, therefore, was not related to gradle dependencies or versions.

Luckily someone will cast some light on this exception!

like image 626
josemigallas Avatar asked Jul 13 '16 10:07

josemigallas


People also ask

What are logging interceptors?

The Logging Interceptor is a pretty trivial Interceptor which just logs to Jakarta Commons Logging or log4j as messages are sent or acknowledged on a broker. The default logging level used is INFO. If you want to increase/reduce the logging you can use change it via commons logging or log4j.

What is interceptor in OkHttp?

Interceptors, according to the documentation, are a powerful mechanism that can monitor, rewrite, and retry the API call. So, when we make an API call, we can either monitor it or perform some tasks. In a nutshell, Interceptors function similarly to airport security personnel during the security check process.

What is HttpLoggingInterceptor level body?

BODY. public static final HttpLoggingInterceptor.Level BODY. Logs request and response lines and their respective headers and bodies (if present).

Is there a logging interceptor for okhttp in Retrofit 2?

While developing your app and for debugging purposes it’s nice to have a log feature integrated to show request and response information. Since logging isn’t integrated by default anymore in Retrofit 2, we need to add a logging interceptor for OkHttp.

What happened to the logging in Retrofit 2?

The logging functionality was removed in Retrofit 2, since the required HTTP layer is now completely based on OkHttp. Since many developers asked for logging capabilities in Retrofit 2, the developers of OkHttp added a logging interceptor in release 2.6.0.

When to add logging to httplogginginterceptor?

Always add logging as the last interceptor, because this will also log the information which was added with previous interceptors. 3. Logging level By default, HttpLoggingInterceptor has 4 levels of logging: BASIC – Logs request and response lines. BODY – Logs request and response lines and their respective headers and bodies (if present).

Does retrofit need to handle exception mapping?

and it does not need to handle any exception mapping. This approach has also one additional benefit — you can have a setup of your networking Retrofit code in one gradle library module and your mapping logic can be in a feature module that has a dependency on this module. You do not need to combine all of this together in one module.


2 Answers

Retrofit 2.1.0 relies on OkHttp 3.3.0, so I would use the same version for the Logging Interceptor (which is part of OkHttp):

compile 'com.squareup.okhttp3:logging-interceptor:3.3.0' 
like image 120
Jorge Pastor Avatar answered Sep 29 '22 16:09

Jorge Pastor


Can you try to add the below dependency

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

And let me know if there is any progress

You can also refer this link.

like image 20
Soham Avatar answered Sep 29 '22 17:09

Soham