从 Android 模拟器访问 localhost:port [英] Accessing localhost:port from Android emulator

查看:37
本文介绍了从 Android 模拟器访问 localhost:port的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的本地机器上运行一个 Web 服务,该服务在 localhost:54722 上运行.

I'm running a web service on my local machine that runs at localhost:54722.

我想从在 Android 模拟器中运行的应用调用该服务.

I want to call the service from an app running in the Android emulator.

我读到在应用程序中使用 10.0.2.2 可以访问 localhost,但它似乎也不适用于端口号.它说 HttpResponseException: Bad Request.

I read that using 10.0.2.2 in the app would access localhost, but it doesn't seem to work with the port number as well. It says HttpResponseException: Bad Request.

推荐答案

您可以使用 IP 地址10.0.2.2"访问您的主机.

You can access your host machine with the IP address "10.0.2.2".

Android 团队就是这样设计的.因此,您的网络服务器可以完美地在本地主机上运行,​​并且您可以通过http://10.0.2.2:<hostport>"从您的 Android 应用程序访问它.

This has been designed in this way by the Android team. So your webserver can perfectly run at localhost and from your Android app you can access it via "http://10.0.2.2:<hostport>".

如果您的模拟器必须通过代理服务器访问互联网,您可以从模拟器的扩展控件屏幕配置自定义 HTTP 代理.打开模拟器,点击更多 ,然后点击设置代理.从这里,您可以定义自己的 HTTP 代理设置.

If your emulator must access the internet through a proxy server, you can configure a custom HTTP proxy from the emulator's Extended controls screen. With the emulator open, click More , and then click Settings and Proxy. From here, you can define your own HTTP proxy settings.

这篇关于从 Android 模拟器访问 localhost:port的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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