使用 React Native 时出现错误:“E/AndroidRuntime:致命异常:OkHttp Dispatcher”

sou*_*ure 6 android react-native

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)
Run Code Online (Sandbox Code Playgroud)

Axios 请求对于一些请求来说工作正常,但是发生了崩溃,之后就没有任何效果了。必须删除该应用程序并重新安装才能使其再次运行。

我尝试过的事情:

  1. 根据此 GitHub 问题,在所有请求中添加了 { 'Connection': 'close' } 但未修复它。https://github.com/facebook/react-native/issues/11016#issuecomment-336685459
  2. 在 Gradle 文件中添加了 okhttp3 依赖项。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"
 }
Run Code Online (Sandbox Code Playgroud)

alt*_*osh 0

我认为这与这个问题有关: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)行:98

if (value.startsWith("\"") && value.endsWith("\"")) {
   value = value.substring(1, value.length - 1)
}
Run Code Online (Sandbox Code Playgroud)

当值仅包含一个双引号 (") 时失败,因为值的长度=1,但它尝试使用 startIndex=1 进行子字符串化

我添加了一个问题,但如果您需要一个快速解决方案,我认为您需要以某种方式注入您自己的固定版本的 JavaNetCookieJar。