【问题标题】:Can't access localhost from Android Windows Emulator (Visual Studio 2015 / Xamarin)无法从 Android Windows 模拟器 (Visual Studio 2015 / Xamarin) 访问 localhost
【发布时间】:2017-02-02 19:11:51
【问题描述】:

我知道我可以通过 10.0.2.2 :portnumber 从 Windows Android Emulator 访问 localhost(我需要从 Xamarin 应用程序访问在 localhost 上运行的这些 Web API 服务我正在VS2015中开发)。

我可以通过 localhost 验证 Web API 的服务运行正常,但不能通过 Android 模拟器验证,此屏幕截图总结了问题:

https://s23.postimg.org/calomedq3/localhost.jpg

根据我的研究,这应该可行。我错过了什么?非常感谢。

screenshot

【问题讨论】:

  • 防火墙中的端口是否开放?
  • 你能否在浏览器中使用 IP (10.0.2.2) 而不是 127.0.0.1 访问 API
  • 我不确定你是否理解这个问题,或者我没有表达清楚。 10.0.2.2 是 Google 的 Android 团队实现的特殊别名,用于从 Android VM 中访问 localhost(即我的开发机器)。因此,我将无法使用 10.0.2.2 从在 localhost(如屏幕截图中)中运行的浏览器访问 API,只能使用“localhost”或 127.0.0.1。在 Aandroid VM 中运行浏览器则相反——“localhost”或 127.0.0.1 在那里毫无意义,因为实例在模拟器中运行并且可以访问虚拟路由器,而不是本地网络。
  • 运行adb shell ifconfig 并检查子网,在我的例子中是 10.0.3.xxx not 10.0.2.xxx
  • 在您的屏幕截图中,您似乎正在连接到不同的网址。特别是最后一个字符是:systemtype=I 而您的 localhost 浏览器显示 systemtype=D

标签: android xamarin localhost emulation


【解决方案1】:

好的,我设法使用“ngrok”访问了 Web API(ngrok 允许您将在本地计算机上运行的 Web 服务器公开到 Internet)。

https://ngrok.com/docs#expose

这可能不是理想的解决方案,但它似乎运作良好。提醒一下:我没有在我的开发机器上使用 IIS 来托管 Web API,我使用的是在默认 localhost:5000 中运行的 Kestrel

附带说明,http://169.254.80.80/ 似乎也可以工作,但它会转到默认的 ISS 网站。因此,我可以通过设置一个重定向到 Kestrel 的新 IIS 站点来使其工作,或者简单地吸收它并运行 Web API 的 o ISS(尽管 Kestrel 的全部目的是避免 ISS,因为 Kestrel 已经过大量优化)。无论如何,ngrok 似乎运作良好,所以我可能会保留它,直到我有更多时间看看替代品。

【讨论】:

    【解决方案2】:

    最终解决方案更新:

    我设法通过 169.254.80.80 在 (MS) Android Emulator 上使其工作。

    我无法仅通过 Kestrel 使其工作(这些是 .NET Core API),必须在 IIS 中的 80 端口上运行 Kestrel,这样就成功了。

    【讨论】:

      猜你喜欢
      • 2015-01-17
      • 1970-01-01
      • 2015-02-19
      • 2016-03-27
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 2018-05-18
      相关资源
      最近更新 更多