如何解决Android Expo Android上API请求出现网络错误
我正在使用Expo开发一个React Native应用,最近遇到了一个奇怪的问题。 API请求在Android设备上不起作用,但是在iOS上完全可以。我尝试连接的服务器不是本地主机。我尝试使用axios和fetch,两者均导致网络错误,并且服务器未收到请求。我还提出了要求打开API的请求,但这些错误也让我遇到了同样的错误,因此我认为问题不在服务器端。
这是请求的代码:
const axiosInstance = axios.create({
timeout: 120000,headers: {
'Accept': 'application/json','Content-Type': 'application/json','Access-Control-Allow-Origin': '*'
}
})
const res = await axiosInstance.post(`${serverHostApi}/merchant/login`,payload)
这是堆栈跟踪:
Network Error
- node_modules\axios\lib\helpers\normalizeHeaderName.js:8:14 in processHeader
* http://192.168.150.71:19001/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&minify=false&hot=false:226876:26 in handleError
- node_modules\event-target-shim\dist\event-target-shim.js:818:20 in EventTarget.prototype.dispatchEvent
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:575:10 in setReadyState
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:389:6 in __didCompleteResponse
- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189:10 in emit
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:425:19 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:112:6 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:373:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:4 in callFunctionReturnFlushedQueue
* [native code]:null in callFunctionReturnFlushedQueue
我正在使用Expo 38.0.0,React Native SDK 38.0.2。 任何帮助将不胜感激!
解决方法
如果仅在android应用中发生这种情况。可能是您不小心从android应用中删除了互联网许可的情况。请检查您在Android清单文件中的权限是否正确。 要进行检查,请转到YourApp / android / app / src / main并打开AndroidManifest.xml 并检查您是否有此行
<uses-permission android:name="android.permission.INTERNET" />
如果没有,请像这样在您的清单文件中添加此行
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
,
我也遇到了这个问题。
您可以使用 SSL Labs 等工具来测试您的 SSL 证书的有效性。
我用这个方法解决了
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。