java.lang.NoSuchMethodError: No virtual method toString(Z)Ljava/lang/String;在Lokhttp3/Cookie类中;或其超级等级

2022-09-05 00:17:53

我在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;在Lokhttp3/Cookie类中;或其超类(声明 'okhttp3.Cookie' 出现在 okhttp3 的 /data/app/com.package-1/base.apk:classes3.dex) 中。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) atokhttp3.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) atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)


答案 1

您正在拉入旧版本的 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!
}

答案 2

我使用了以下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'

推荐