Fetch / Axios 在 React Native 中严重崩溃(但仅针对某些 URL)
Fetch / Axios crashing hard in React Native (but only for certain URLs)
我的应用程序在执行 某些 API 调用时严重崩溃,我将其缩小到这一点:
- 这不是 HTTP 与 HTTPS 的区别
- 我最终使用了两种不同的模拟 APIs,令我惊讶的是,一种有效,另一种无效。两者基本相同,请参见下面的片段
- WIFI 或蜂窝网络上的行为相同
- axios 中的相同问题而不是 fetch
- 调用
catch
块时出现无法描述的网络错误,但应用程序仍然严重崩溃
环境: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")
我的应用程序在执行 某些 API 调用时严重崩溃,我将其缩小到这一点:
- 这不是 HTTP 与 HTTPS 的区别
- 我最终使用了两种不同的模拟 APIs,令我惊讶的是,一种有效,另一种无效。两者基本相同,请参见下面的片段
- WIFI 或蜂窝网络上的行为相同
- axios 中的相同问题而不是 fetch
- 调用
catch
块时出现无法描述的网络错误,但应用程序仍然严重崩溃
环境: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")