React Native获取停止,没有返回或错误



我目前正在构建一个React Native Android应用程序,我试图在我的代码中做一个简单的获取。

const response = await fetch('http://localhost:8081/assets/src/...',{
method: 'GET'
})

这个调用绝对没有输出。没有错误,没有返回。在登录Metro服务器时,甚至没有请求进入。代码完全停止运行。我用XMLHttpRequest试过,但结果几乎相同。唯一的区别是服务器接收请求。onerroronload永远不会被调用。

我已经试过了:

  • android:usesCleartextTraffic="true"添加到manifest xml
  • inlineRequires: true,在metro配置
  • 使用XMLHttpRequest
  • <uses-permission android:name="android.permission.INTERNET" />是设置

我真的希望有人能帮助我!

我目前package.json:

{
"name": "myapp",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"@react-native-async-storage/async-storage": "^1.17.7",
"@tensorflow/tfjs": "^3.18.0",
"@tensorflow/tfjs-react-native": "^0.8.0",
"async-storage": "^0.1.0",
"aws-sdk": "^2.1170.0",
"expo": "^45.0.0",
"expo-asset": "^8.5.0",
"expo-camera": "^12.2.0",
"expo-constants": "~13.1.1",
"expo-file-system": "^14.0.0",
"expo-gl": "^11.3.0",
"expo-gl-cpp": "^11.3.0",
"expo-modules-core": "^0.9.2",
"ffmpeg-kit-react-native": "^4.5.2",
"geolib": "^3.3.3",
"graphql": "^16.5.0",
"graphql-ws": "^5.9.1",
"react": "17.0.2",
"react-native": "0.68.2",
"react-native-base64": "^0.2.1",
"react-native-battery": "^0.1.18",
"react-native-device-info": "^10.0.0",
"react-native-fs": "^2.20.0",
"react-native-geolocation-service": "^5.3.0",
"react-native-maps": "^1.0.0",
"react-native-polyfill-globals": "^3.1.0",
"react-native-vision-camera": "^2.13.5",
"text-encoding": "^0.7.0"
},
"devDependencies": {
"@babel/core": "^7.12.9",
"@babel/eslint-parser": "^7.18.2",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/runtime": "^7.12.5",
"@react-native-community/eslint-config": "^2.0.0",
"babel-jest": "^26.6.3",
"eslint": "^7.32.0",
"jest": "^26.6.3",
"metro-react-native-babel-preset": "^0.67.0",
"react-test-renderer": "17.0.2"
},
"jest": {
"preset": "react-native"
}
}

如果你从Postman和从浏览器得到的响应看起来不错,那么很可能是你使用了模拟器无法理解的localhost。

尝试使用您的IP地址(运行模拟器的PC上的IP地址)。

所以不是:fetch('http://localhost:8081/assets/src

use:fetch('http://localIPaddressFromYourPC:8081/assets/src