我的Ionic应用程序无法从Android模拟器访问我的本地Node服务器 [英] My Ionic app can't access my local Node server from Android emulator

查看:165
本文介绍了我的Ionic应用程序无法从Android模拟器访问我的本地Node服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用电容器在Android模拟器上首次运行我的React/Ionic应用程序.该应用程序应使用Axios连接到我的本地节点服务器.

I'm trying to run for the first time my React/Ionic app on an Android Emulator using Capacitor. The app should connect to my local Node server using Axios.

我的应用在模拟器上成功启动时,所有服务器请求均失败,并显示 Msg:Error:Network Error

While my app started successfully on the emulator, all server requests failed with Msg: Error: Network Error

这是我执行的步骤:

  • 我将Axios配置为连接到 http://10.0.2.2:3001 而不是 localhost ,但是请求仍然失败

  • I configured Axios to connect to http://10.0.2.2:3001 instead of localhost, but the requests still fail

我在Android Emulator Chrome浏览器中打开了网址 http://10.0.2.2:3001/api ,并且在连接到我的服务器时成功并返回响应

I opened the url http://10.0.2.2:3001/api in the Android Emulator Chrome browser, and it succeeded in connecting to my server and returning a response

我尝试将以下conf添加到我的capacitor.config.json文件中,但是我的应用仍然失败

I tried adding to my capacitor.config.json file the following conf, but my app still fails

"server": {
  "allowNavigation": ["10.0.2.2"]
}

您还有其他建议,我应该尝试使我的应用程序连接到本地服务器吗?谢谢

Would you have other advices I should try to make my app connect to my local server? Thank you

推荐答案

搜索后,我在另一个

  • 问题出在我的应用使用 http 而不是 https 请求(Android默认从API级别28开始)这一事实.
    • The problem comes from the fact the my app uses http instead of https requests which Android forbids by default, starting with API level 28.

    为允许 http 请求,我在我的应用程序的 AndroidManifest.xml

    To allow http requests, I added android:usesCleartextTraffic="true" to my app's AndroidManifest.xml

    参考: https://developer.android.com/guide/topics/manifest/application-element#usesCleartextTraffic

    这篇关于我的Ionic应用程序无法从Android模拟器访问我的本地Node服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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