当前位置: 首页 > 知识库问答 >
问题:

如何修复预期的Android API级别21但在Android中为19的问题

莫英喆
2023-03-14

在我的应用程序中,我希望从服务器获取数据,为了连接到我改型使用的服务器,OkHttp
但在运行应用程序时,向我显示强制关闭错误
在android api 21中不是错误,但在api 21下面显示强制关闭错误

ApiClient类代码:

class ApiClient constructor(private val deviceUUID: String) {

    private val apiServices: ApiServices

    init {
        //Gson
        val gson = GsonBuilder()
            .setLenient()
            .create()

        //Http log
        val loggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.level =
            if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE

        //Http Builder
        val clientBuilder = OkHttpClient.Builder()
        clientBuilder.interceptors().add(loggingInterceptor)
        clientBuilder.addInterceptor { chain ->
            val request = chain.request()
            request.newBuilder().addHeader(AGENT_NAME, AGENT_VALUE).build()
            chain.proceed(request)
        }
        clientBuilder.addInterceptor { chain ->
            val request = chain.request()
            request.newBuilder().addHeader(X_CLIENT_VERSION, APP_VERSION_NAME).build()
            chain.proceed(request)
        }
        clientBuilder.addInterceptor { chain ->
            val request = chain.request()
            request.newBuilder().addHeader(UUID_NAME, deviceUUID).build()
            chain.proceed(request)
        }

        //Http client
        val client = clientBuilder
            .readTimeout(NETWORK_CONNECTIONS_TIME, TimeUnit.SECONDS)
            .writeTimeout(NETWORK_CONNECTIONS_TIME, TimeUnit.SECONDS)
            .connectTimeout(NETWORK_CONNECTIONS_TIME, TimeUnit.SECONDS)
            .retryOnConnectionFailure(true)
            .build()

        //Retrofit
        val retrofit = Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
            .build()

        //Init apiServices
        apiServices = retrofit.create(ApiServices::class.java)
    }

    companion object {
        private var apiClient: ApiClient? = null

        fun getInstance(deviceUUID: String): ApiClient =
            apiClient ?: synchronized(this) {
                apiClient ?: ApiClient(deviceUUID).also {
                    apiClient = it
                }
            }
    }

    /**
     * Send apiServices to ApisUseCase for initialize all of apis
     */
    fun apisUseCase(): ApisUseCase {
        return ApisUseCase(apiServices)
    }
}

显示此行的错误:。此方法上的build()val client=clientBuilder

我把这些代码添加到gradle中。建筑

compileOptions {
    sourceCompatibility = '1.8'
    targetCompatibility = '1.8'
}

但再次显示我的错误!

错误消息:

java.lang.ExceptionInInitializerError
    at okhttp3.OkHttpClient.newSslSocketFactory(OkHttpClient.java:263)
    at okhttp3.OkHttpClient.<init>(OkHttpClient.java:229)
    at okhttp3.OkHttpClient$Builder.build(OkHttpClient.java:1015)
    at com.app.android.data.services.ApiClient.<init>(ApiClient.kt:56)
    at com.app.android.ui.splash.SplashPresenterImpl.checkUpdate(SplashPresenterImpl.kt:18)
    at com.app.android.ui.splash.SplashActivity.onCreate(SplashActivity.kt:40)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
    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:5019)
    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:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    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)

格拉德尔。构建代码:

    apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'androidx.navigation.safeargs'
apply plugin: 'kotlin-kapt'
apply plugin: 'io.fabric'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.app.android"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 100
        versionName "2.5.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
    }
}

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0-beta01'
    implementation 'androidx.core:core-ktx:1.2.0-alpha02'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.viewpager:viewpager:1.0.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.core:core:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    //JWT decoder
    implementation 'com.auth0.android:jwtdecode:1.2.0'
    //Anko lib
    implementation "org.jetbrains.anko:anko-commons:0.10.8"
    implementation "org.jetbrains.anko:anko-design:0.10.8"
    //Rx
    implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
    implementation "io.reactivex.rxjava2:rxjava:2.2.8"
    //OkHttp
    implementation 'com.squareup.okhttp3:okhttp:3.14.1'
    implementation "com.squareup.okhttp3:logging-interceptor:3.14.0"
    //Retrofit
    implementation "com.squareup.retrofit2:retrofit:2.5.0"
    implementation "com.squareup.retrofit2:adapter-rxjava2:2.5.0"
    implementation "com.squareup.retrofit2:converter-gson:2.5.0"
    //Gson
    implementation 'com.google.code.gson:gson:2.8.5'
    //Image
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
    //Calligraphy
    implementation 'io.github.inflationx:calligraphy3:3.1.0'
    implementation 'io.github.inflationx:viewpump:1.0.0'
    //Navigation
    implementation 'androidx.navigation:navigation-fragment-ktx:2.1.0-alpha05'
    implementation 'androidx.navigation:navigation-ui-ktx:2.1.0-alpha05'
    //Preferences lib
    implementation 'com.github.MrNouri:GoodPrefs:1.0'
    //Multiple sizes
    implementation 'com.intuit.sdp:sdp-android:1.0.6'
    // OnBoarding
    implementation 'com.codemybrainsout.onboarding:onboarder:1.0.4'
    //Alerter
    implementation 'com.tapadoo.android:alerter:4.0.2'
    //Fabric answer
    implementation('com.crashlytics.sdk.android:answers:1.4.7@aar') { transitive = true }
    //Fabric crash
    implementation('com.crashlytics.sdk.android:crashlytics:2.9.9@aar') { transitive = true }
    //Adjust
    implementation 'com.adjust.sdk:adjust-android:4.17.0'
    implementation 'com.android.installreferrer:installreferrer:1.0'
    //Google and firebase services
    implementation 'com.google.android.gms:play-services-analytics:16.0.8'
    implementation 'com.google.firebase:firebase-core:16.0.9'
    implementation 'com.google.firebase:firebase-messaging:17.6.0'
    //Support MultiDex
    implementation 'androidx.multidex:multidex:2.0.1'
    //Room
    implementation 'androidx.room:room-runtime:2.1.0'
    annotationProcessor 'androidx.room:room-compiler:2.1.0'
    //Android svg and noneOldAndroid
    implementation 'com.caverock:androidsvg-aar:1.3'
    implementation 'com.nineoldandroids:library:2.4.0'
    //Animations
    implementation 'com.daimajia.easing:library:2.0@aar'
    implementation 'com.daimajia.androidanimations:library:2.3@aar'
}
apply plugin: 'com.google.gms.google-services'

我怎样才能修好它?

共有3个答案

文志学
2023-03-14

对于所有仍然需要旧版本Android库的人,您需要使用OkHttp 3.12. x分支。该分支的最新版本是3.12.6。我已经测试并确认该版本适用于三星平板电脑SM-T116NU(Android 19)。

以下是OkHttp的摘录:

OkHttp 3.12. x分支支持Android 2.3(API级别9)和Java7。这些平台缺乏对TLS 1.2的支持,不应使用。但是因为升级很困难,我们将在2020年12月31日之前将关键修复备份到3.12. x分支。

柳胜
2023-03-14

OKHTTP刚刚放弃了对Android 4的支持

https://github.com/square/okhttp/issues/4481

您必须为您的应用程序使用旧版本或sperate分支。

更多信息,请参阅杰西·威尔逊的这篇中篇文章:https://medium.com/square-corner-blog/okhttp-3-13-requires-android-5-818bb78d07ce

仲法
2023-03-14

对于minSDK低于21,将OkHttp版本更改为3.12.12,如下所示-

  //OkHttp
  implementation ("com.squareup.okhttp3:okhttp:3.12.12"){
      force = true //API 19 support
  }
  implementation 'com.squareup.okhttp3:logging-interceptor:3.12.12'

它应该工作正常!

 类似资料:
  • 我怎么可能得到这个信息?这没有任何意义。我正在使用

  • 我使用改型2和OkHttp3从服务器获取数据,使用Min_SDK 17时出错,我的设备的API也是17 我尝试了这个答案:如何修复预期的Android API级别21,但在Android中是19 我也试过这个答案https://github.com/square/okhttp/issues/4597#issuecomment-461204144 但我也犯了同样的错误。 我的Gradle构建类 我的

  • 所以我一直得到这个“;”预期的错误,但似乎无法发现它在哪里,请您帮助。非常感谢。 从反应中导入反应,{useState,组件};从反应中导入{导航};从反应中导入{StyleSheet,TouchableHighlight,尺寸,文本,视图,Touchable不透明度,安全区域视图,图像, 按钮,TouchableWithouse tFeedback,滚动视图,文本输入,动画};从反应本机导入{c

  • 我只是从我当地的回购协议中克隆了一个Android项目,我很难“构建”它。也许它错过了一些外部图书馆,但我不知道。 在开始等级构建时,我得到以下信息: 我试着检查“清单”文件,但我找不到。我完全迷路了... 我很确定这是一个配置错误,因此我可以在这里找到所有的配置文件: settings.gradle代码 (来自项目文件夹内)的输出如下:

  • 问题内容: 今天,我面临着一个巨大的错误,该错误使我无法在手机上运行示例项目。 当Android Studio构建项目时,它首先显示以下目标: 在构建过程中有许多这样的日志语句。但是,他们总是停滞不前。对于,我似乎从未收到过日志声明。 而是,跟着此错误: 现在有许多这样的错误。它们甚至出现在v4支持库中的类上,这确实令人困惑。 最后,这些错误以新的语句结尾: 我不确定它们是否适用于我的情况。我什至

  • 问题内容: 我该如何解决这个错误 错误:任务’:app:dexDebug’的执行失败。com.android.ide.common.process.ProcessException:org.gradle.process.internal.ExecException:处理’command’/Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Conten