在我的項目中,我使用了OkHttp(版本2.5.0)作爲我的網絡請求庫。但是前幾天,我嘗試將Retrofit2作爲我的應用程序的網絡請求庫的一部分,正如您所知的那樣,新的Retrofit2庫對OkHttp(版本3.2.0)的依賴。因此,當我的應用在android下的是Android 5.0的版本總是會引起崩潰(放大版運作良好,並不會造成崩潰),崩潰堆棧信息顯示如下:
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: java.lang.NoClassDefFoundError: okhttp3.OkHttpClient$Builder
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.comingx.athit.client.ServiceGenerator.<clinit>(ServiceGenerator.java:22)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.comingx.athit.ui.fragments.CircleNativeFragment.onViewCreated(CircleNativeFragment.java:105)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.os.Handler.handleCallback(Handler.java:808)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.os.Handler.dispatchMessage(Handler.java:103)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.os.Looper.loop(Looper.java:193)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5348)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at dalvik.system.NativeStart.main(Native Method)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: java.lang.NoClassDefFoundError: okhttp3.OkHttpClient$Builder
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.comingx.athit.client.ServiceGenerator.<clinit>(ServiceGenerator.java:22)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.comingx.athit.ui.fragments.CircleNativeFragment.onViewCreated(CircleNativeFragment.java:105)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.os.Handler.handleCallback(Handler.java:808)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.os.Handler.dispatchMessage(Handler.java:103)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.os.Looper.loop(Looper.java:193)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5348)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at dalvik.system.NativeStart.main(Native Method)
從堆棧跟蹤,我想這OkHttp(3.2.0版本)有一些與OkHttp(版本2.5.0)衝突。下面我會給我的外部庫配置:
compile 'com.squareup.okhttp:okhttp:2.5.0'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
希望任何知道這個問題的人都能幫我一個忙!萬分感謝!
更新1: 這是我ServiceGenerator.java:
package com.comingx.athit.client;
import com.comingx.athit.model.Constants;
import com.comingx.athit.model.modelmanager.ModelManagerSingleton;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ServiceGenerator {
private static OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(
new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder().addHeader("Cookie", ModelManagerSingleton.getNative_cookie()).build();
return chain.proceed(request);
}
}
).build();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(Constants.V3.API_PREFIX)
.addConverterFactory(GsonConverterFactory.create());
public static <T> T createService(Class<T> serviceClass) {
Retrofit retrofit = builder.client(httpClient).build();
return retrofit.create(serviceClass);
}
}
要貼上'com.comingx.athit.client.ServiceGenerator'的代碼? – srain
它看起來像OkHttp 3被設計爲允許與早期的OkHttp搭配,如通過使用不同的包名稱來判斷。因此,我傾向於猜測:(1)實際上你的運行時類路徑中沒有OkHttp 3,或者(2)你的類路徑中的OkHttp 3不完整,或者(3)'com.comingx.athit .client.ServiceGenerator',不管那是什麼,都需要一個不同的版本,或許是更早的版本。 –
我認爲你可以刪除2行'compile'com.squareup.okhttp:okhttp:2.5.0' compile'com.squareup.okhttp3:okhttp:3.2.0'' – BNK