Retrofit日志拦截器异常

22

我正在尝试使用Retrofit启用日志记录,但是我遇到了以下异常:

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)

这是我这样做的方法:

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

我尝试只添加了一个拦截器,但仍然崩溃。我正在使用以下依赖项:

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'

我在这里做错了什么?

编辑: 这很尴尬...问题解决了。 我没有改变任何东西,我正在修订所有提交,并且RetrofitClientbuild.gradle都没有更改。 因此,问题与gradle依赖项或版本无关。

幸运的是,有人将对这个异常进行一些解释!

4个回答

38

Retrofit 2.1.0 依赖于 OkHttp 3.3.0,因此我建议使用相同版本的 Logging Interceptor(它是 OkHttp 的一部分):

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

6

您是否可以尝试添加以下依赖项?

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

请告诉我是否有任何进展。

您还可以参考此链接


1
在我的情况下,这对依赖项解决了问题:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'

无论如何,问题在于依赖一致性...

0

如果你遇到了同样的问题,而且这个问题不是随机出现的话,那么尝试使用Invalidate/Restart重启Android Studio。也许这个问题会在未来的更新中得到解决。

1. 卸载之前的构建。
2. 文件-> Invalidate/Cache -> Invalidate and restart。


Rajesh,这在理论上如何帮助解决问题?你能解释一下吗? - Sigmund

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接