簡體   English   中英

java.lang.NoSuchMethodError: 沒有虛擬方法 toString(Z)Ljava/lang/String; 在 class Lokhttp3/Cookie; 或其超類

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

我在 Android 項目中使用 retrofit (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: 沒有虛擬方法 toString(Z)Ljava/lang/String; 在 class Lokhttp3/Cookie; 或其超類('okhttp3.Cookie' 的聲明出現在 /data/app/com.package-1/base.apk:classes3.dex 中)在 okhttp3.JavaNetCookieJar.saveFromResponse(JavaNetCookieJar.java:45) 在 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)在 okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:197) 在 okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:502) 在 java.util.concurrent.Thr eadPoolExecutor.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 彈出后出現的。

解決方案是 go 將以下內容添加到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中的版本

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM