Java NoClassDefFoundError:解析失败:Lokhttp3/internal/Platform

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39545629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:14:11  来源:igfitidea点击:

NoClassDefFoundError: Failed resolution of: Lokhttp3/internal/Platform

javaandroidretrofit2okhttp3

提问by Huy Tower

I'm using Retrofit2library.

我正在使用Retrofit2图书馆。

I already tried to update latest version : Retrofit2, Gson, Rxjava, OKHttp, HttpLoggingInterceptor ... in build.gradlefile

我已经尝试更新最新版本:Retrofit2、Gson、Rxjava、OKHttp、HttpLoggingInterceptor ... in build.gradlefile

build.gradein application

build.grade申请中

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',         {
    exclude group: 'com.android.support', module: 'support-annotations'
})

// Default - Android Component
testCompile 'junit:junit:4.12'
compile 'com.android.support:design:24.2.1'
compile 'com.android.support:appcompat-v7:24.2.1'

// Retrofit2 + Gson
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
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.3.0'
compile 'com.squareup.okhttp3:okhttp:3.4.1'

// RxJva
compile 'io.reactivex:rxandroid:1.2.0'
compile 'io.reactivex:rxjava:1.1.4'
}

But I got error

但我有错误

 > Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lokhttp3/internal/Platform;
at okhttp3.logging.HttpLoggingInterceptor$Logger.log(HttpLoggingInterceptor.java:112)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:160)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at internal.NetworkModule.intercept(NetworkModule.java:150)

in below code

在下面的代码中

Interceptor provideInterceptor(final Context context, final PreferenceStore preferenceStore) {
    Interceptor headerAuthorizationInterceptor = new Interceptor() {
        @Override
        public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
            // Get Android ID
            String android_id = Settings.Secure.getString(context.getContentResolver(),
                    Settings.Secure.ANDROID_ID);

            // Get Application Version
            String appVersion = "";
            try {
                PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
                appVersion = packageInfo.versionName;
            } catch (PackageManager.NameNotFoundException e) {
                Timber.e("Cannot get app version");
            }

            /**
             * Add header with following attributes as agree with Web Server
             * - Token
             * - "Android"
             * - Release version
             * - Model
             * - App version
             * - Android ID
             */
            String token = preferenceStore.getAuthToken();

            Timber.i("X-Asukabu-Token: %s", token);

            // Add header to request
            Request request = chain.request();
            if (token != null)
                request.headers().newBuilder()
                        .add("X-Asukabu-Token", token)
                        .add("X-Asukabu-Client-OS", "Android")
                        .add("X-Asukabu-Client-OS-Version", Build.VERSION.RELEASE)
                        .add("X-Asukabu-Client-Device", Build.MODEL)
                        .add("X-Asukabu-Client-App-Version", appVersion)
                        .add("X-Asukabu-Client-Device-ID", android_id)
                        .add("Accept","*/*")
                        .build();
            else
                request.headers().newBuilder()
                        .add("X-Asukabu-Client-OS", "Android")
                        .add("X-Asukabu-Client-OS-Version", Build.VERSION.RELEASE)
                        .add("X-Asukabu-Client-Device", Build.MODEL)
                        .add("X-Asukabu-Client-App-Version", appVersion)
                        .add("X-Asukabu-Client-Device-ID", android_id)
                        .add("Accept","*/*")
                        .build();

            // ERROR IN THIS LINE : LINE 150
            return chain.proceed(request);
        }
    };

    return headerAuthorizationInterceptor;
}

build.gradlein Project :

build.gradle在项目中:

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.2.0-rc2'

    // Dagger 2
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

People who knows why I got this error?

知道为什么我收到此错误的人?

Please tell me how to fix it,

请告诉我如何修复它,

Thank you,

谢谢,

采纳答案by Michael Katkov

I guess, the problem is here:

我想,问题出在这里:

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

Try to make that:

尝试做到这一点:

compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.squareup.okhttp3:okhttp:3.4.1'