Axios和Android模拟器出现网络错误 [英] Network error with axios and android emulator

查看:66
本文介绍了Axios和Android模拟器出现网络错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个React-Native应用程序,它与提供API的NodeJS后端一起使用.

I got a React-Native application working with a NodeJS backend which serves an API.

我的React-Native前端正在使用Expo和Axios来路由我的NodeJS API(使用Hapi,Joi,Knex),这将(例如)更新我的数据库(MySQL).

My React-Native front is using Expo and Axios to hit on a route of my NodeJS API (using Hapi, Joi, Knex), which will (for the example) update my DB (MySQL).

我的iOS模拟器一切正常.但是,在Android Emulator上,我的某些命中路线不起作用""时出现以下错误消息:网络错误-createError中的node_modules/axios/lib/core/createError.js:16:24 (实际上,它起作用了,但是前端未检测到它...)

Everything works properly with my iOS simulator. However, on Android Emulator, SOME of my hits on route ""does not work"" with the following error message : Network Error - node_modules/axios/lib/core/createError.js:16:24 in createError (actually, it worked, but the front does not detect it...)

这很奇怪,因为就像我说的那样,这仅适用于我的某些路线.我将 http://localhost:8000/api 更改为 http://10.0.2.2:8000/api ,以确保Android仿真器可以访问我的API,并且这部分还可以.

This is strange, because like I said, this is ONLY for some of my route. I changed the http://localhost:8000/api to http://10.0.2.2:8000/api to be sure that Android Emulator access my API, and it is ok for this part.

越野车路线在iOS上正常运行,并且在Insomnia/Postman( localhost:8000/api/sendMail )上正常运行.它可以在Android Emulator上运行,但应用程序无法检测到它.

The buggy route is working properly on iOS, and is working properly on Insomnia / Postman ( localhost:8000/api/sendMail ). It is working on Android Emulator, but the application does not detect it.

这是我的代码的示例:

FRONT-按下我的"SendEmail"按钮:

/* Button.js */
const sendAndEmailPromise = await sendEmail(this.state.email);

console.log(sendAndEmailPromise); // Output : NOTHING (not undefined, not null, N O T H I N G).

if (sendAndEmailPromise.status === 200) {
    // handle it
} else (sendAndEmailPromise.status === 403) {
    // etc. for each of my codeStatus that can be returned by my API 
}

/* MyAPI.js */
export function sendEmail(mail) {
    return axiosInstance
    .post(`/sendEmail`, null, {
      params: {
        mail
      },
    })
    .then(response => response) // Will not enter here
    .catch(error => {
       console.log(error); // Will output : NETWORK ERROR
    });
}

返回-这是sendEmail承诺:

// Will return true of false :
const isMailSend = await sendTheEmail(mail);
console.log(isMailSend); // Output : TRUE and I receive the email on my gmail, so Android Emulator HIT the route.

if (isMailSend) {
  return handler
    .response({
      data: {
        message: "Email sent.",
      },
    })
    .code(200);
} else {
  // Handle it and return another response
}

我希望我的工作到现在为止一切正常(实际上发生了...),并获得该代码的状态,而不是网络错误".

I expect my front to now that everything works fine (which actually happened...) and get the code status of that, instead of a "Network error".

更多,Axios是否有可能获得另一个错误级别?更具体些.

More, is it possible with Axios to get another level of error ? Something more specific.

一个GitHub问题并不完全相同,但似乎具有相同的含义: https://github.com/axios/axios/issues/973

A GitHub issue which is not totally the same but seems to relate something equivalent : https://github.com/axios/axios/issues/973

谢谢.

推荐答案

您在这里处理两个问题:

You are dealing with two problems here:

1)您的仿真器无法正常工作,因为它无法将单词"localhost"解析为ip.就像使用localhost2.com一样,它无法解决问题.这必须指向服务器192.x.x.x的本地IP

1) your emulator doesnt work because it cannot resolve the word "localhost" to an ip. is like using localhost2.com it will not resolve to something. This must be pointed to the local ip of your server 192.x.x.x

2)您的服务器必须绑定到0.0.0.0,因为通过将其绑定到本地主机无法将其解析为本地请求(如192.x.x.x).如果您解决了绑定问题,那么您的仿真器将有可能看到服务器,邮递员也将看到该服务器.

2) you server must be binded to 0.0.0.0 because by binding it to localhost it cannot resolve to local requests like 192.x.x.x. If you fix the binding your emulator would have the possibility to see the server and postman will too.

这篇关于Axios和Android模拟器出现网络错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆