java.lang.NoSuchMethodError: No virtual method toString(Z)Ljava/lang/String; in class Lokhttp3/Cookie; or its super classe

java.lang.NoSuchMethodError: No virtual method toString(Z)Ljava/lang/String; in class Lokhttp3/Cookie; or its super classe

我在一个 Android 项目中使用改造 (2.7.2) 和 OkHttp 版本 (4.4.0),并且我在请求时遇到崩溃。

依赖关系:

kapt("com.squareup.moshi:moshi-kotlin-codegen:1.9.2")
implementation 'com.squareup.moshi:moshi:1.9.2'     
implementation 'com.squareup.retrofit2:retrofit:2.7.2'
implementation 'com.squareup.retrofit2:converter-moshi:2.7.2'

implementation("com.squareup.okhttp3:okhttp:4.4.0")
implementation("com.squareup.okhttp3:okhttp-tls:4.4.0")
implementation "com.squareup.okhttp3:logging-interceptor:4.4.0"

崩溃:

java.lang.NoSuchMethodError: No virtual method toString(Z)Ljava/lang/String; in class Lokhttp3/Cookie; or its super classes (declaration of 'okhttp3.Cookie' appears in /data/app/com.package-1/base.apk:classes3.dex) at okhttp3.JavaNetCookieJar.saveFromResponse(JavaNetCookieJar.java:45) at com.facebook.react.modules.network.ReactCookieJarContainer.saveFromResponse(ReactCookieJarContainer.java:36) at okhttp3.internal.http.HttpHeaders.receiveHeaders(HttpHeaders.kt:207) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:85) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:74) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100) at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:197) at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:502) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)

您正在引入旧版本的 okhttp-urlconnection,它与当前版本的 OkHttp 核心库不兼容。

您可以通过添加对 okhttp-urlconnection 的显式依赖来修复:

implementation("com.squareup.okhttp3:okhttp-urlconnection:4.4.1")

或者采用 OkHttp 的新 BOM 版本:

dependencies {
   implementation(platform("com.squareup.okhttp3:okhttp-bom:4.4.1"))
   implementation("com.squareup.okhttp3:okhttp")              // No version!
   implementation("com.squareup.okhttp3:okhttp-urlconnection") // No version!
}

我使用了以下 okhttp3 版本,醒来后感觉很好...

implementation 'com.squareup.okhttp3:okhttp:4.7.2'
implementation 'com.squareup.okhttp3:logging-interceptor:4.4.1'
implementation 'com.squareup.okhttp3:okhttp-urlconnection:4.4.1'

请确保 build.gradle 文件对于以下依赖项具有相同的版本,如果您的版本低于此版本并且出现以上错误而不是更改它们的版本并且您的问题已解决。:

实施'com.squareup.okhttp3:logging-interceptor:4.8.0' 实施 'com.squareup.okhttp3:okhttp:4.8.0'

更改版本如下:

实施'com.squareup.okhttp3:logging-interceptor:3.4.1' 实施 'com.squareup.okhttp3:okhttp:3.4.1'

我想补充一点,这个错误是在从 React Native 中的 Expo 弹出后出现的。

解决方案是将以下内容添加到 android/app/build.gradle(内部依赖项):

    compile "com.squareup.okhttp3:okhttp:4.2.1"
    compile "com.squareup.okhttp3:logging-interceptor:4.2.1"
    compile "com.squareup.okhttp3:okhttp-urlconnection:4.2.1"

它们看起来像这样:

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    //noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:+"  // From node_modules
   
    // you can insert it here
    compile "com.squareup.okhttp3:okhttp:4.2.1"
    compile "com.squareup.okhttp3:logging-interceptor:4.2.1"
    compile "com.squareup.okhttp3:okhttp-urlconnection:4.2.1"
   
}

(注:查看Maven Repository

中的版本