简体   繁体   English

react-native 的“网络请求失败”(即使使用 `android:usesCleartextTraffic=“true”`)

[英]“Network request failed” with react-native (even with `android:usesCleartextTraffic=“true”`)

I did npx react-native init AwesomeProject to create a new project (I'm following the official get-started tutorial . When I simply run npx react-native start and then npx react-native run-android without tweaking anything everything works fine. But I need to make an API call, so I tried copying and pasting the example from the official network tutorial (only without the try/catch, so I can see the errors) into App.js (right after the imports):我做了npx react-native init AwesomeProject来创建一个新项目(我正在按照官方入门教程进行操作。当我简单地运行npx react-native start然后npx react-native run-android 。 API 调用,所以我尝试将官网教程中的示例(仅没有 try/catch,所以我可以看到错误)复制并粘贴到 App.js 中(在导入之后):

async function getMoviesFromApiAsync() {
    let response = await fetch(
        'https://reactnative.dev/movies.json'
    );
    let json = await response.json();
    console.log(json.movies);
    return json.movies;
}

Then right below it I added a call to that function:然后在它的正下方,我添加了对 function 的调用:

getMoviesFromApiAsync();

That gives me a Network request failed error.这给了我一个Network request failed错误。 This is the full error message I see on Terminal:这是我在终端上看到的完整错误消息:

Fri May 08 2020 10:23:00.325]  WARN     Possible Unhandled Promise Rejection (id: 0):
TypeError: Network request failed
onerror@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:28005:31
dispatchEvent@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:34133:31
setReadyState@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:33217:33
__didCompleteResponse@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:33044:29
emit@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:3420:42
__callFunction@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:2748:49
http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:2470:31
__guard@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:2702:15
callFunctionReturnFlushedQueue@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:2469:21
callFunctionReturnFlushedQueue@[native code]

I googled around and ended up trying the following:我四处搜索并最终尝试了以下操作:

1) I added android:usesCleartextTraffic="true" to AndroidManifest.xml 1)我将android:usesCleartextTraffic="true"添加到 AndroidManifest.xml

2) I added a bunch of permissions to AndroidManifest.xml: 2)我给AndroidManifest.xml添加了一堆权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

3) I added android:networkSecurityConfig="@xml/network_security_config" to AndroidManifest.xml 3)我将android:networkSecurityConfig="@xml/network_security_config"添加到 AndroidManifest.xml

4) I tried setting a proxy (10.0.2.2) and port (8081), as suggested in here 4)我尝试设置代理(10.0.2.2)和端口(8081),如此处所建议

5) I tried deleting the app from the emulator and reinstalling it, as well as clearing the cache ( npx react-native start --reset-cache ) 5)我尝试从模拟器中删除应用程序并重新安装它,以及清除缓存( npx react-native start --reset-cache

Nothing seems to work.似乎没有任何效果。 I still get the same "Network request failed" message.我仍然收到相同的“网络请求失败”消息。

I'm using React Native 0.62.2 on a Pixel 2 API 28 emulator running Android 9.0, on macOS Catalina.我在 macOS Catalina 上运行 Android 9.0 的 Pixel 2 API 28 模拟器上使用 React Native 0.62.2。

I'm stupid.我真笨。 I've been at this for three full days and only now I realized that the problem is that the emulator didn't have access to the internet (even after adding android.permission.INTERNET to the manifest).我已经在这里呆了整整三天,直到现在我才意识到问题是模拟器无法访问互联网(即使在将android.permission.INTERNET添加到清单之后)。 The solution was to change the DNS address of my network, as detailed in this answer .解决方案是更改我的网络的 DNS 地址,如本答案中所述。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法发送 HTTP 和 axios 甚至设置 android:usesCleartextTraffic="true" and.network security config in React native - Can't send HTTP with axios even set android:usesCleartextTraffic="true" and network security config in React native React-Native Android“网络请求失败” - React-Native Android 'Network Request failed' Android上的React-Native fetch()网络请求失败 - React-Native fetch() Network Request Failed on Android React-native Android模拟器 - POST因TypeError失败:网络请求失败 - React-native Android emulator - POST fails with TypeError: Network request failed 类型错误:尝试从 android 模拟器访问本地 API 时网络请求失败。 React-Native - TypeError: Network Request Failed when trying to hit local API from android emulator. React-Native react-native提取在具有HTTP远程服务器后端的Android设备上引发“网络请求失败”错误 - react-native fetch throws “Network request failed” error on Android Devices with backend in http remote server Fetch 在 React-Native 0.14 中抛出“网络请求失败” - Fetch throws "Network request failed" in React-Native 0.14 react-native中的“网络请求失败”和“ XMLHttpRequest.chr.onerror” - “Network request failed” and “XMLHttpRequest.chr.onerror” in react-native 在android上反应本机获取网络请求失败 - react native Fetch Network request failed on android React Native Android 中的网络请求失败 - Network Request Failed in React Native Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM