有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓 java。lang.NoSuchMethodError:没有虚拟方法toString(Z)Ljava/lang/String;Lokhttp3/Cookie类;还是它的上等货

我在一个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)


共 (3) 个答案

  1. # 1 楼答案

    我使用了以下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'
    
  2. # 2 楼答案

    您正在引入旧版本的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!
    }
    
  3. # 3 楼答案

    请确保这个版本。gradle文件为下面的依赖项提供了相同的版本,如果您有以下版本且出现以上错误,请更改它们的版本,您的问题已得到解决:

    实现'com。收拾一下。okhttp3:日志侦听器:4.8.0' 实现'com。收拾一下。okhttp3:okhttp:4.8.0'

    使用以下命令更改版本:

    实现'com。收拾一下。okhttp3:日志侦听器:3.4.1' 实现'com。收拾一下。okhttp3:okhttp:3.4.1'