简体   繁体   English

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

[英]Network Error when use axios in React Native

This is long-standing problem for me and I can't find solution at all.这对我来说是长期存在的问题,我根本找不到解决方案。

Within my react native project(react-native-cli), I send request to server using axios package.在我的 react native 项目(react-native-cli)中,我使用 axios package 向服务器发送请求。 But when I'm testing with my android phone, it gives [Error: Network Error].但是当我用我的 android 手机进行测试时,它会给出 [错误:网络错误]。 With emulator, it's okay.用模拟器就好了。

I'm sure this is not back-end problem because I've tested with several servers.我确定这不是后端问题,因为我已经用多台服务器进行了测试。 And I also used fetch instead but got same error.而且我也使用了 fetch 但得到了同样的错误。

Here is version information.这是版本信息。

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

Sometimes, it's working on android phone too.有时,它也适用于 android 手机。 This problem is biggest mystery and I suffered for a long time.这个问题是最大的谜,我受了很长时间。

Somebody who experienced this issue, please alive me.经历过这个问题的人,请让我活着。 Thank you for careful reading.感谢您的仔细阅读。

Sometimes, it's not working even with android emulator.有时,即使使用 android 仿真器,它也无法正常工作。 But after deleted original emulator and created new emulator, it's working.但是在删除原始模拟器并创建新的模拟器后,它就可以工作了。

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);
        })

I think this is a problem with http and https request.我认为这是 http 和 https 请求的问题。 SO to enable https request try the one below :所以要启用 https 请求,请尝试以下方法:

The easy way to implement this is to use this attribute to your AndroidManifest.xml where you allow all http for all requests:实现这一点的简单方法是在您的AndroidManifest.xml中使用此属性,您可以在其中允许所有请求的所有http

android:usesCleartextTraffic="true"

尝试在 AndroidManifest.xml 中添加波纹管代码

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

对于 Android,不要使用: localhost ,而是使用10.0.2.2

I have tried adding access network state permission and usesCleartextTraffic and both of the solutions didn't help.我尝试添加访问网络状态权限并使用CleartextTraffic,但这两种解决方案都没有帮助。 I am still getting this error.Out of all my endpoints (30+), I am only getting this error while sending a post request to logout with a refresh token as parameter.我仍然收到此错误。在我的所有端点(30+)中,我仅在发送发布请求以使用刷新令牌作为参数注销时收到此错误。 The rest of the api is working fine. api的其余部分工作正常。

I have found the problem.我发现了问题。 The endpoint returns 205 and this creates an error returning Network Error instead of success.端点返回 205,这会创建一个返回网络错误而不是成功的错误。 I am thinking to tell my backend to change the response status code to 200.我想告诉我的后端将响应状态代码更改为 200。

My problem was that I was using the host url as http://127.0.0.1 while running my app on an emulator.我的问题是我在模拟器上运行我的应用程序时将主机 url 用作http://127.0.0.1

With emulators, the use of "localhost" OR "127.0.0.1" does not apply since emulator tries only within its scope.对于模拟器,“localhost”或“127.0.0.1”的使用不适用,因为模拟器仅在其范围内尝试。

If you are running your app in a local env, make sure that both your device and the server are in the same network and use the IP address of the server as the host url.如果您在本地环境中运行您的应用程序,请确保您的设备和服务器都在同一个网络中,并使用服务器的 IP 地址作为主机 url。

In my case it was http://192.168.8.101:8080就我而言,它是http://192.168.8.101:8080

Was facing the same issue & solved it by doing:面临同样的问题并通过以下方式解决:

1- Find the IP Address of your machine. 1- 找到您机器的 IP 地址。

2- Replace your localhost address with your IP address ** 2-用您的 IP 地址替换您的本地主机地址**

 replace ==>  'http://localhost:8000/api/download'

 with ==> 'http://192.120.16.108:8000/api/download'

我也遇到了同样的问题,你是怎么解决的???

暂无
暂无

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

相关问题 使用 Axios 或在 android 中获取时反应本机抛出网络错误 - React Native throw Network Error when using Axios or Fetch in android 错误:网络错误 Axios - Image React Native - Error: Network Error Axios - Image React Native Axios React Native 网络错误 Android Expo - Axios Network Error in React Native with Expo in Android Axios POST 请求在 React Native 中将图像添加到 FormData 结构时给出“网络错误” - Axios POST request gives a “Network Error” when adding image to FormData structure in React Native 使用 axios 访问在 localhost 上运行的 api 时,React Native 应用程序 GET 请求导致网络错误 - React Native app GET request resulting in network error when using axios to reach for api running on localhost React Native https axios resquest 在 Android 上出现网络错误,但在 iOS 上没有 - React Native https axios resquest gets Network Error on Android but not iOS 在 Android 物理设备上反应本机 axios 后请求网络错误 - React native axios post request network error on Android physical device React Native Axios 上传图片返回 Network Error on Android - React Native Axios upload image return Network Error on Android 如何解决 React Native Android Axios 网络错误? - How to resolve React Native Android Axios Network Error? 错误:在 React Native 中使用 axios 的网络错误,仅在 iOS 和 Android 中出现 - Error: Network Error using axios in React Native, issue in only in iOS and Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM