[英]Expected Android API level 21+ but was 19 while using Retrofit 2, OkHttp3
I'm using Retrofit 2 and OkHttp3 to data from server and I get error while using Min_SDK 17 and my device's API is 17 also我正在使用Retrofit 2 和 OkHttp3从服务器获取数据,在使用Min_SDK 17时出现错误,而我的设备的 API 也是17
I tried this answer: How to fix Expected Android API level 21+ but was 19 in Android我试过这个答案: How to fix Expected Android API level 21+ but was 19 in Android
Also I tried this answer https://github.com/square/okhttp/issues/4597#issuecomment-461204144我也试过这个答案https://github.com/square/okhttp/issues/4597#issuecomment-461204144
but I get the same error.但我得到同样的错误。
my Gradle.Build class我的 Gradle.Build class
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "some"
minSdkVersion 17
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
}
compileOptions {
targetCompatibility = "8"
sourceCompatibility = "8"
}
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:support-v13:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0"
implementation "com.squareup.okhttp3:okhttp:3.11.0"
implementation "com.squareup.okhttp3:okhttp-urlconnection:3.11.0"
implementation "com.squareup.okhttp3:logging-interceptor:3.11.0"
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'net.alhazmy13.hijridatepicker:library:2.0.2'
implementation group: 'com.github.msarhan', name: 'ummalqura-calendar', version: '1.1.9'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.google.android.gms:play-services-places:16.1.0'
implementation 'com.github.jaiselrahman:FilePicker:1.0.2'
implementation 'com.github.bumptech.glide:glide:4.5.0'
implementation 'com.squareup.okhttp3:okhttp:3.13.1'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.loopj.android:android-async-http:1.4.9'
implementation 'com.google.android.gms:play-services-auth-api-phone:16.0.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
my API Client我的 API 客户端
public class APIClient {
private static Retrofit retrofit;
private static OkHttpClient okHttpClient;
public static Retrofit getInstanceRetrofit(){
if(okHttpClient==null) {
initOkHttp();
}
if(retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(Const.URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
private static void initOkHttp() {
int REQUEST_TIMEOUT = 60;
OkHttpClient.Builder httpClient = new OkHttpClient().newBuilder()
.connectTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(interceptor);
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(@NonNull Chain chain) throws IOException {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.addHeader("Accept", "application/json")
.addHeader("Content-Type", "application/json");
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
okHttpClient = httpClient.build();
}
}
my Logs我的日志
1-03 06:09:07.850 2827-2827/some
E/AndroidRuntime: FATAL EXCEPTION: main
Process: some, PID: 2827
java.lang.ExceptionInInitializerError
at okhttp3.OkHttpClient.newSslSocketFactory(OkHttpClient.java:296)
at okhttp3.OkHttpClient.<init>(OkHttpClient.java:262)
at okhttp3.OkHttpClient.<init>(OkHttpClient.java:235)
at some.Network.APIClient.initOkHttp(APIClient.java:40)
at some.Network.APIClient.getInstanceRetrofit(APIClient.java:25)
at some.MvpImplementation.DropdownImpInteractor.getDropdowns(DropdownImpInteractor.java:18)
at some.MvpImplementation.DropdownImpPresenter.requestDropdowns(DropdownImpPresenter.java:21)
at some.Views.LoginActivity.onCreate(LoginActivity.java:87)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Expected Android API level 21+ but was 19
at okhttp3.internal.platform.AndroidPlatform.buildIfSupported(AndroidPlatform.java:238)
at okhttp3.internal.platform.Platform.findPlatform(Platform.java:202)
at okhttp3.internal.platform.Platform.<clinit>(Platform.java:79)
at okhttp3.OkHttpClient.newSslSocketFactory(OkHttpClient.java:296)
at okhttp3.OkHttpClient.<init>(OkHttpClient.java:262)
at okhttp3.OkHttpClient.<init>(OkHttpClient.java:235)
at some.Network.APIClient.initOkHttp(APIClient.java:40)
at some.Network.APIClient.getInstanceRetrofit(APIClient.java:25)
at some.MvpImplementation.DropdownImpInteractor.getDropdowns(DropdownImpInteractor.java:18)
at some.MvpImplementation.DropdownImpPresenter.requestDropdowns(DropdownImpPresenter.java:21)
at some.Views.LoginActivity.onCreate(LoginActivity.java:87)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
If you guys have still crash on Android 4.0 and above
, here is quick solution:如果你们在Android 4.0 and above
上仍然崩溃,这里是快速解决方案:
implementation("com.squareup.retrofit2:retrofit:2.7.1")
implementation("com.squareup.okhttp3:okhttp:3.12.8") {
force = true
}
implementation "com.squareup.okhttp3:logging-interceptor:3.12.8"
Above code tested and verified on Android 4.4
以上代码在Android 4.4
上测试和验证
Thanks谢谢
The problem is that you have added OkHttp
dependency twice.问题是您已经添加了两次OkHttp
依赖项。
In your build.gradle
you have:在您的build.gradle
,您有:
dependencies {
...
implementation "com.squareup.okhttp3:okhttp:3.11.0"
...
implementation "com.squareup.okhttp3:okhttp:3.13.1"
}
Starting with version 3.13.0 they removed support for Android < 5. You just need to delete the从版本 3.13.0 开始,他们删除了对 Android < 5 的支持。您只需删除
implementation "com.squareup.okhttp3:okhttp:3.13.1"
line and it should work fine行,它应该可以正常工作
In your gradle file you have:在您的 gradle 文件中,您有:
implementation "com.squareup.okhttp3:okhttp:3.11.0"
The docs say:文档说:
https://square.github.io/okhttp/ https://square.github.io/okhttp/
OkHttp works on Android 5.0+ (API level 21+) and on Java 8+. OkHttp 适用于 Android 5.0+(API 级别 21+)和 Java 8+。
The OkHttp 3.12.x branch supports Android 2.3+ (API level 9+) and Java 7+. OkHttp 3.12.x 分支支持 Android 2.3+(API 级别 9+)和 Java 7+。 These platforms lack support for TLS 1.2 and should not be used.这些平台缺乏对 TLS 1.2 的支持,因此不应使用。 But because upgrading is difficult we will backport critical fixes to the 3.12.x branch through December 31, 2020.但由于升级很困难,我们将在 2020 年 12 月 31 日之前将关键修复移植到 3.12.x 分支。
So you could try:所以你可以尝试:
implementation "com.squareup.okhttp3:okhttp:3.12.0"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.