gpt4 book ai didi

android - 在 React Native 中使用 axios 时出现网络错误

转载 作者:行者123 更新时间:2023-12-03 17:08:26 24 4
gpt4 key购买 nike

这对我来说是长期存在的问题,我根本找不到解决方案。

在我的 react native 项目(react-native-cli)中,我使用 axios 包向服务器发送请求。
但是当我用我的安卓手机进行测试时,它给出了[错误:网络错误]。
用模拟器就好了。

我确定这不是后端问题,因为我已经用多台服务器进行了测试。
而且我也使用了 fetch 但得到了同样的错误。

这是版本信息。

"axios": "^0.19.0",
"react": "16.9.0",
"react-native": "0.61.5",

有时,它也适用于安卓手机。
这个问题是最大的谜,我受了很长时间。

经历过这个问题的人,请让我活着。
感谢您的仔细阅读。

有时,即使使用 android 模拟器也无法正常工作。但是在删除原始模拟器并创建新的模拟器后,它就可以工作了。
const serverUrl = "https://server.com";
axios.post(serverUrl + "/api/candidates/login", {
"email": this.state.email ,
"password": this.state.password
}, {
headers: {
'Content-Type': 'application/json'
}
}).then((response) => {
console.log(response.data);
}).catch((error) => {
console.log(error);
})

最佳答案

我认为这是 http 和 https 请求的问题。所以要启用 https 请求,请尝试以下方法:

实现这一点的简单方法是将此属性用于您的AndroidManifest.xml。你允许所有http对于所有请求:

android:usesCleartextTraffic="true"

关于android - 在 React Native 中使用 axios 时出现网络错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59600004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com