18

I'm using retrofit (2.7.2) and OkHttp version (4.4.0) in an Android project and I'm facing a crash on a request.

Dependencies:

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"

Crash:

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)

Bruno
  • 3,872
  • 4
  • 20
  • 37
A B
  • 235
  • 1
  • 2
  • 7
  • latest version of okhttp3 is 4.4.1. change from 4.4.0 to 4.4.1. refer this https://search.maven.org/search?q=g:com.squareup.okhttp3 – jose praveen Mar 09 '20 at 08:55
  • @JosePraveen updated okhttp version to 4.4.1 still getting same crash – A B Mar 09 '20 at 10:18

4 Answers4

35

You're pulling in an old version of okhttp-urlconnection, and it is incompatible with the current version of the OkHttp core library.

You can fix by adding an explicit dependency on okhttp-urlconnection:

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

Or by adopting OkHttp’s new BOM for versions:

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!
}
Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
12

I used following okhttp3 versions and woked fine for me...

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'
ultra.deep
  • 1,699
  • 1
  • 19
  • 23
  • I'm getting `The server may not support the client's requested TLS protocol versions: (TLSv1.2). You may need to configure the client to allow other protocols to be used.` Is the package outdated? – Jaap Weijland Nov 09 '21 at 12:19
1

Please make sure that build.gradle file hase a same version for belowed dependencies and if you have this below version and getting above error than change version for them and your issue has been solved.:

implementation 'com.squareup.okhttp3:logging-interceptor:4.8.0' implementation 'com.squareup.okhttp3:okhttp:4.8.0'

Change the version with below :

implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1' implementation 'com.squareup.okhttp3:okhttp:3.4.1'

MEGHA DOBARIYA
  • 1,622
  • 9
  • 7
0

I wanted to add that this error was showing up after ejecting from Expo in React Native.

The solution was to go to add the following to android/app/build.gradle (inside dependencies):

    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"

They would look like this:

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"
   
}

(Note: Check the versions in the Maven Repository

Jose A
  • 10,053
  • 11
  • 75
  • 108