【问题标题】:Fetch / Axios crashing hard in React Native (but only for certain URLs)Fetch / Axios 在 React Native 中严重崩溃(但仅适用于某些 URL)
【发布时间】:2020-10-13 09:44:19
【问题描述】:

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

  • 这不是 HTTP 与 HTTPS 的事情
  • 我最终使用了两种不同的模拟 API,令我惊讶的是,一种有效,另一种无效。两者基本相同,见下sn-p
  • 在 WIFI 或蜂窝网络上的行为相同
  • 在 axios 而不是 fetch 中存在同样的问题
  • catch 块被调用时出现无法描述的网络错误,但随后应用仍然严重崩溃

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

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

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

【问题讨论】:

  • 请告知问题的解决方案。
  • 我发布了我的修复作为答案@TanviAgarwal -

标签: axios fetch react-native-android


【解决方案1】:

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

我的解决方法是更改​​我在 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")

【讨论】:

    猜你喜欢
    • 2021-08-07
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 2018-07-20
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多