离子框架:Android 模拟器无法访问主机上的服务 [英] Ionic Framework : Android Emulator unable to access services on host machine

查看:41
本文介绍了离子框架:Android 模拟器无法访问主机上的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施 Ionic 示例教程http://ccoenraets.github.io/ionic-tutorial/install-ionic.html

I am trying to implement the Ionic sample tutorial available at http://ccoenraets.github.io/ionic-tutorial/install-ionic.html

我能够在 Android 虚拟设备上部署示例应用程序.但是,该应用程序无法访问笔记本电脑上托管的 REST 服务.我试图通过将localhost"更改为10.0.2.2"来访问该服务.我还在 config.xml 中添加了以下内容

I am able to deploy sample app on Android Virtual Device. However the app isn't able to access the REST services hosted on the laptop. I am trying to access the service by changing "localhost" to "10.0.2.2". I have also added the below in the config.xml

有人可以告诉我如何解决这个问题吗?

Can someone please let me know how to resolve this issue?

推荐答案

如果您使用的是cordova 的最新版本之一,则必须安装cordova 插件白名单:

If you're using one of the latest versions of cordova you must install the cordova plugin whitelist:

cordova plugin add cordova-plugin-whitelist

如果您使用选项 --save,它将在您的 config.xml 文件中添加一个部分:

if you use the option --save it will add a section to your config.xml file:

cordova plugin add cordova-plugin-whitelist --save

您可以在此处阅读有关最近cordova更新的重要变化的一些有用信息.

You can read some useful info here about important change in recent cordova updates.

你需要有这个:

<access origin="*" />

在您的 config.xml 文件中设置.

set in your config.xml file.

如果您使用的是 android 模拟器,您必须在 10.0.2.2 中更改 localhost,当然,您必须添加端口(如果与默认值不同):80.

If you're using the android emulator you have to change localhost in 10.0.2.2 and, of course, you have to add the port if it is different from the default: 80.

参考此处.

如果您使用 Genymotion(比安卓模拟器好得多),IP 地址将会为:10.0.3.2 + 端口号.

If you're using Genymotion (much better than the android emulator) the ip address is going to be: 10.0.3.2 + the port num.

示例

Android 模拟器:http://10.0.2.2:6050/api/checkstatus
Genymotion:http://10.0.3.2:6050/api/checkstatus

如果您想获得 android 模拟器的调试信息,请从命令行运行:

If you want to have debug infos for the android emulator run this from the command line:

adb -s emulator-5554 logcat

如果您在 IIS Express 中运行您的网络服务器,您的请求可能无法到达主机.这里有几个选项.我最喜欢的解决方案是使用 iisexpress-proxy.
您可以将其安装为 npm 包:

If you're running you web server in IIS Express chances are your request cannot reach the host. There are a few options here. My favourite solution is to use iisexpress-proxy.
You can install it as an npm package:

npm install -g iisexpress-proxy

并指定代理端口运行它:

and run it specifying the proxy port:

iisexpress-proxy 6050 to 3000

现在您可以更改模拟器的端口:

Now you can change your port for your emulators:

Android 模拟器:http://10.0.2.2:3000/api/checkstatus
Genymotion:http://10.0.3.2:3000/api/checkstatus

可能最好的解决方案是使用真正的 android 设备并使用 chrome inspect 调试您的应用.

Probably the best solution is to use a real android device and debug your app with chrome inspect.

你必须:

  • 在您的手机上启用 USB 调试
  • 在 Chrome 浏览器 (chrome://inspect/#devices) 中访问此链接
  • enable USB debugging on your mobile
  • visit this link in chrome browser (chrome://inspect/#devices)

一旦您的设备显示在列表中,您就可以按 F12 并查看您在浏览器控制台中遇到的错误类型.

Once your device show in the list you can hit F12 and see what kind of errors your getting in the browser console.

您甚至可以使用端口转发转发请求到内部服务器;基本上,您可以通过您要定义的虚拟端口访问您笔记本电脑上的托管服务.

You can even use the port forwarding forward request to an internal server; basically you can access your hosted services on your laptop through a virtual port you're going to define.

这篇关于离子框架:Android 模拟器无法访问主机上的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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