Error: 'E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher' in using react native

Viewed 1551
2020-12-01 11:20:24.357 16113-16534/com.innerfit.app E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
    Process: com.innerfit.app, PID: 16113
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
        at java.lang.String.substring(String.java:2064)
        at okhttp3.JavaNetCookieJar.decodeHeaderAsJavaNetCookies(JavaNetCookieJar.kt:98)
        at okhttp3.JavaNetCookieJar.loadForRequest(JavaNetCookieJar.kt:59)
        at com.facebook.react.modules.network.ReactCookieJarContainer.loadForRequest(ReactCookieJarContainer.java:44)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:75)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:71)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184)
        at okhttp3.RealCall$AsyncCall.run(RealCall.kt:136)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)

Axios request works fine for a few requests but this crash happens and nothing works after that. Have to delete the application and reinstall it in order to make it work again.

Things I tried:

  1. Added { 'Connection': 'close' } in all requests according to this GitHub issue but doesn't fix it. https://github.com/facebook/react-native/issues/11016#issuecomment-336685459
  2. Added okhttp3 dependencies in Gradle file. https://github.com/facebook/react-native/issues/27250#issuecomment-573111088
 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"
 }
1 Answers

I think it is related to this issue: https://github.com/square/okhttp/issues/6897

okhttp_4.2.x -> JavaNetCookieJar.kt (https://github.com/square/okhttp/blob/okhttp_4.2.x/okhttp-urlconnection/src/main/java/okhttp3/JavaNetCookieJar.kt) Line: 98

if (value.startsWith("\"") && value.endsWith("\"")) {
   value = value.substring(1, value.length - 1)
}

Fails when the value contains only one double quote ("), because the length of the value=1, but it tries to substring with the startIndex=1

I added an issue, but If you need a quick solution I think you need to somehow inject your own fixed version of JavaNetCookieJar.

Related