Fetch / Axios 在 React Native 中严重崩溃(但仅针对某些 URL)

Fetch / Axios crashing hard in React Native (but only for certain URLs)

我的应用程序在执行 某些 API 调用时严重崩溃,我将其缩小到这一点:

环境:Android10(实际设备),RN 0.61.5

下面的两个调用只执行一个简单的 HTTP GET,这会导致返回 JSON 片段。一个有效,另一个导致严重崩溃。

async foo() {
    try {

        // this endpoint CRASHES my app
        const r = await fetch("http://jsonplaceholder.typicode.com/todos/1");
        
        // this endpoint works just fine
        //const r = await fetch("http://echo.jsontest.com/key/value/one/two");

    } catch(e) {
        console.log("Invoked, but the app still crashes hard right after");
    }
}

好的,我在 Logcat 中发现了一个提示 - 使用 OkHttp3 似乎导致了某种版本控制冲突,这种冲突只出现在其中一个响应中(可能是由于 CORS headers,但是那是纯粹的、毫无根据的猜测 ;)。

我的解决方法是在 build.gradle 中将我对 OkHttp3 的依赖从

更改为
implementation "com.squareup.okhttp3:okhttp:4.7.2"

api(platform("com.squareup.okhttp3:okhttp-bom:4.7.2"))
api("com.squareup.okhttp3:okhttp")
api("com.squareup.okhttp3:logging-interceptor")