如何从 Android 模拟器连接到我的 http://localhost Web 服务器
我可以在 Android 模拟器中执行哪些操作才能将其连接到位于 http://localhost
或 http://127.0.0.1
的本地主机 Web 服务器页面?
我已经尝试过,但模拟器仍然接受我的请求,就像 Google 搜索本地主机一样,或更糟糕的是,它说在我的网络服务器正常运行时没有找到该页面。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
尽管阅读了此处和其他地方的所有答案,但我还是花费了几个小时来尝试调试此问题,因为即使在 Chrome 浏览器中,地址
10.0.2.2
也不起作用。如果您遇到同样的情况,请参阅以下分步指南来尝试调试并希望解决您的问题。检查模拟器网关是否为 10.0.2.2
在模拟的 Android 中,转到“设置”>“ WiFi,检查是否连接到 AndroidWiFi 热点(代表您的主机),然后点击底部的高级,然后检查网关地址:它应该指向 10.0.2.2 。如果没有,那么您遇到了另一个问题,也许更改代理设置可以解决您的问题,请参阅此处自 2022 年以来如何使用 Android Studio 执行此操作,因为代理设置现在已隐藏:如何在新版本的 Android Studio 中的模拟器中配置代理?
检查是否您可以从主机访问您的服务器
只需打开 Web 浏览器并输入
http://localhost:
即可查看您的本地 Web 应用程序是否可以访问。如果没有,那么您的本地服务器参数可能有问题。检查是否可以从模拟器访问您的服务器
打开 Chrome 浏览器,并将其指向
http://10.0.2.2:
(对于 genymotion,替换为http://10.0 .3.2:<端口>
)。如果您的网络应用程序出现,那就太好了,您就完成了。如果没有,请测试以下其他步骤以查明根本问题。使用另一台服务器进行测试
如果您的 Web 应用程序可以从主机访问,但不能在模拟器内部访问,则根本原因可能是您的本地服务器出于某种原因(可能是出于安全原因)限制对某些接口的访问。
要检查这一点,请尝试使用其他服务器,只需一个简单的 HTTP 服务器即可,例如带有
nodejs
的http-server
或python -m http.server 8000
使用 Python 3。然后,尝试从模拟器的 Chrome 浏览器访问这个简单的服务器,例如
http://10.0.2.2:8000
。如果有效,则确认您的本地服务器限制对某些接口的访问。您需要阅读本地服务器的文档以扩大权限。例如,就我而言,我的服务器是 angular-cli (AngularJS),默认情况下限制仅向本地主机提供服务。为了使其工作,我必须使用
ngserve --disable-host-check --host 0.0.0.0
而不是仅仅ngserve
,如 这个其他问题 。 --host 0.0.0.0 指示网络服务器为所有接口提供服务。类似的参数可以提供给大多数网络服务器。另一种方法可能是禁用一些未使用的适配器,尤其是 VPN 等虚拟适配器。
您的 Android 应用对明文的权限
现在,您的 Web 应用应该可以使用 Chrome 应用通过 URL
http://10.0.2.2:
从模拟器内部访问。最后一个难题是在您的 Android 应用程序中添加访问 10.0.2.2 的权限,尤其是如果您的本地网络服务器未配备 SSL 证书(这是本地开发网络服务器最有可能出现的情况 - 只需检查https://localhost:
有效,或者仅在主机上使用http://localhost:
)。这将允许您的 Android 应用程序访问您的本地网络服务器,就像 Chrome 一样。自 Android 9 (API 28) 及以上版本起,需要添加特定权限以从 Android 应用访问明文(即
http://
)。有多种方法可以配置您的 Android 应用程序以添加此权限,请参阅:https://stackoverflow.com/a/50834600/1121352< /a>结论
从 Android 模拟器访问主机可能很棘手,但通过仔细的逐步调试,在大多数情况下可以克服该问题。
本教程仅涵盖从 Android 模拟器内部访问/到达主机上的本地 Web 服务器的问题,但一旦修复此问题,即使可以访问,Web 应用程序也可能仍然无法正常运行。一个例子是体验无限的加载循环。要在解决可达性后进一步调试问题,您可以在 Chrome 浏览器中使用
chrome://inspect
附加到 Android 模拟器内的 Android WebView,并对其进行实时调试,就像它在您的计算机上呈现一样。最后一种选择(可能更快)是付费订阅 ngrok 等服务,但免费版本毫无用处,因为它们必须在 Android 应用程序的 Web 视图之外的 Web 浏览器中打开 Web 应用程序。
Despite reading all the answers here and elsewhere, I have lost several hours trying to debug this issue, as the address
10.0.2.2
did not work, even in Chrome browser. If the same is happening to you, here is a step-by-step guide to try to debug and hopefully fix your issue.Check emulator gateway is 10.0.2.2
Inside the emulated Android, go to Settings > WiFi, check if it is connected to AndroidWiFi hotspot (which represents your host computer), and then click on Advanced at the bottom, then check the Gateway address: it should point to 10.0.2.2 . If not, then you have another issue, maybe changing proxy settings can fix your issue, see here how to do that with Android Studio since 2022, as the proxy setting is now hidden away: How to configure proxy in emulators in new versions of Android Studio?
Check if your server is accessible from your host computer
Simply open a web browser and type
http://localhost:<port>
to see if your local web app is accessible. If not, then you likely have an issue with your local server parameters.Check if your server is accessible from the emulator
Open Chrome browser, and point it to
http://10.0.2.2:<port>
(for genymotion, replace withhttp://10.0.3.2:<port>
). If your web app shows up, great, you're done. If not, then test the other steps below to pinpoint the root issue.Test with another server
In case your web app can be accessed from your host computer, but not inside the emulator, the root cause can be that your local server is restricting access to some interfaces for some reason, likely for security reasons.
To check this, try to use another server, just a simple HTTP server will do, such as
http-server
withnodejs
, orpython -m http.server 8000
with Python 3.Then, try to access this simple server from your emulator's Chrome browser, eg,
http://10.0.2.2:8000
. If it works, then this confirms that your local server is restricting access to some interfaces. You need to read your local server's documentation to broaden permissions.For example, in my case, my server was
angular-cli
(AngularJS), which by default restricts serving only to localhost. To make it work, I had to useng serve --disable-host-check --host 0.0.0.0
instead of justng serve
, as suggested in this other question. The --host 0.0.0.0 instructs the webserver to serve all interfaces. Similar arguments can be provided to most webservers.An alternative might be to disable some unused adapters, especially virtual ones such as VPNs.
Your Android app permissions to cleartext
Now, your web app should be accessible from inside the emulator, using Chrome app, with the URL
http://10.0.2.2:<port>
. The last piece of the puzzle is to add permissions in your Android app to access 10.0.2.2 and especially cleartext if your local webserver is not equipped with a SSL certificate (the most likely scenario for a local development webserver - just check ifhttps://localhost:<port>
works or onlyhttp://localhost:<port>
from the host computer). This will allow your Android app to access your local webserver, just like Chrome does.Adding specific permissions to access cleartext (ie,
http://
) from your Android app is necessary since Android 9 (API 28) upwards. There are several ways to configure your Android app to add this permission, see: https://stackoverflow.com/a/50834600/1121352Conclusion
Accessing the host from the Android emulator can be tricky, but by careful step-by-step debugging, it can be possible to overcome the issue in most cases.
This tutorial only covers the issue of accessing/reaching a local webserver on the host computer from inside an Android emulator, but once this is fixed, the webapp may remain dysfunctional, even if reachable. One example is to experience an infinite loading loop. To debug further issues once reachability is resolved, you can use
chrome://inspect
in the Chrome Browser to attach to the Android WebView inside the Android emulator and live debug it as if it was rendered on your computer.A last alternative, probably faster, is to get a paid subscription to services such as ngrok, but the free version is useless as they necessarily open the webapp in a web browser, outside of your Android app's webview.
对于我的 Mac OS mountain Lion 设备:
完美运行!
For My Mac OS mountain Lion device :
Works perfect !
如果您使用 Android 模拟器:
您可以通过以下 IP 连接到您的 PC 本地主机:
10.0.2.2:{本地主机的端口}
=>如果您在 xamp 中设置机器端口,则必须使用该端口。就我而言10.0.2.2:2080
您也可以使用您的网络适配器 IP。在 CMD 中写入
ipconfig
并找到您的适配器 IP 地址:如果模拟器无法连接到此 IP,请关闭模拟器并通过 AVD Manager 中的
冷启动
将其打开:如果您使用 Genymotion :
您可以通过此 IP 连接到计算机本地主机:
10.0.3.2:{port number}
或者您的适配器 IP 地址,如我上面所解释的:在我的情况下:
192.168.1.3:2080
If you using Android Emulator :
You can connect to your Pc localhost by these IPs :
10.0.2.2:{port of your localhost}
=> if you set your machine port in xamp you must use that port . In my case10.0.2.2:2080
Also you can use your network adapter IP .In CMD write
ipconfig
and find your adapter ip address :If emulator can not connect to this IPs close the emulator an open it by
cold boot
from AVD Manager :If you using Genymotion :
You can connect to machine localhost by this IP :
10.0.3.2:{port number}
Or your adapter IP address as I explained above: in my case :
192.168.1.3:2080
我需要找出模拟器“Nox App Player”的系统主机 IP 地址。我是这样算出来的:
172.17.100.2
。ip link show
命令以显示所有网络接口。特别令人感兴趣的是 eth1 接口ifconfig eth1
命令,显示网络为172.17.100.15/255.255.255.0
172.17.100.1
开始的地址>,在“172.17.100.2”上获得点击。不确定防火墙是否会干扰,但在我的情况下不会干扰,也许这可以帮助其他人弄清楚其他模拟器的情况。
I needed to figure out the system host IP address for the emulator "Nox App Player". Here is how I figured out it was
172.17.100.2
.ip link show
command to show all network interfaces. Of particular interest was the eth1 interfaceifconfig eth1
command, shows net as172.17.100.15/255.255.255.0
172.17.100.1
, got a hit on `172.17.100.2'. Not sure if a firewall would interfere but it didn't in my caseMaybe this can help someone else figure it out for other emulators.
允许 PWA 安装
首先,安装 Android 调试桥:
像往常一样启动 Android 模拟器,例如:
然后,配置 反向代理位于Android模拟器的桥上,它将
localhost
HTTP请求转发到运行在localhost
服务器上的适当端口(例如8000)您的主机,反之亦然:在
localhost:8000
上提供渐进式 Web 应用程序 (PWA)或
127.0.0.1:8000
将可安装并连接到其service-worker.js
。 而不允许从 IP 地址安装 PWA <代码>10.0.2.2。警告:每次 Android 模拟器调用之后都需要重新发出
adb reverse tcp:8000 tcp:8000
。因此,启动 Android 模拟器的 bash 脚本,然后是反向代理,将如下所示:
Allowing PWA installation
First of all, install the Android debug bridge:
Start your Android emulator as usual, e.g.:
Only then, configure a reverse proxy on the bridge of the Android emulator that will forward
localhost
HTTP requests to the appropriate port (e.g. 8000) of thelocalhost
server running on your host computer and vice versa:A progressive web application (PWA) being served on
localhost:8000
or
127.0.0.1:8000
will be installable and connect to itsservice-worker.js
. Whereas PWA installation is not allowed from IP address10.0.2.2
.Caveat:
adb reverse tcp:8000 tcp:8000
needs to be reissued after each Android emulator evocation.Hence, a bash script to launch an Android emulator, followed by a reverse proxy, would look like this:
接受的答案是正确的,但对我来说不起作用。我必须在主机上关闭公司 VPN 客户端的情况下创建虚拟设备。这是完全可以理解的,因为许多公司网络使用以 10 开头的地址(专用网络范围),这可能会干扰特殊地址 10.0.2.2
The accepted answer is correct, but didn't work in my case. I had to create the virtual device with the company VPN-client on the host machine turned off. This is quite understandable as many company networks use adresses starting with 10 (private network range), which could interfere with the special address 10.0.2.2
如果您在 Windows 中,您可以转到 simbol 系统并编写 ipconfig 并检查分配给您的计算机的 IP。
If you are in windows you can go to simbol system and write ipconfig and check what ip is assigned to your machine.
对于任何尝试访问本地 IIS 服务器 (ASP.NET) 的人
对于我来说,接受的答案还不够。我必须在 applicationhost.config 中添加 127.0.0.1 的绑定,该配置位于我的 ASP.NET 解决方案的根目录中。
FOR ANYONE TRYING TO REACH A LOCAL IIS Server (ASP.NET)
For me, the accepted answer was not enough. I had to add a binding for 127.0.0.1 in the applicationhost.config, which was at the root of my ASP.NET solution.
我不知道,也许这个主题已经解决了,但是当我最近尝试在 Windows 机器上执行此操作时,我遇到了很多困难。
所以我的解决方案非常简单。我已经下载了这个软件
http://www.lenzg.net/rinetd/rinetd.html 按照他们的指示操作关于如何进行端口转发,然后成功地将我的 Android 设备连接到 make asp.net localhost 项目并停止在我的断点上。
我的 rinetd.conf 文件:
其中 10.1.1.20 是我的本地主机 IP,82 和 1234 是我的端口
我还创建了沐浴文件
为了方便起见,您可以将该文件放入 rined 文件夹中。我的浴文件:
启动此软件后,启动 aps.net 服务器并尝试从 Android 设备或本地网络中的任何设备进行访问(例如计算机 ABC 启动 putty),您将看到一切正常。无需去路由器设置或做任何其他复杂的事情。
我希望这会对您有所帮助。享受。
I do not know, maybe this topic is already solved, but when I have tried recently do this on Windows machine, I have faced with lot of difficulties.
So my solution was really simple. I have downloaded this soft
http://www.lenzg.net/rinetd/rinetd.html followed their instructions about how to make port forwarding and then successfully my android device connected to make asp.net localhost project and stopped on my breaking point.
my rinetd.conf file:
Where 10.1.1.20 is my localhost ip, 82 and 1234 my ports
Also I have craeted bath file
for easy life yournameofbathfile.bat, put that file inside rinedfolder. My bath file:
After starting this soft, start your aps.net server and try to access from android device or any device in your local network(for example Computer ABC starts putty) and you will see that everything works. No need to go to router setting or do any other complicated things.
I hope this will help you. Enjoy.
2023 答案:
我有一个 Express 应用程序在 localhost:5001 上侦听,并且在
my-real-api.com
上有一个生产 api,所以我使用:然后在其他地方使用:
2023 answer:
I have an express app listening on localhost:5001, and a production api at
my-real-api.com
so I use:and then everywhere else:
就我而言,我正在运行本地服务器,并开发一个针对 Android、iOS、Web 的 flutter 应用程序。这可能对你有帮助。 - 此处
In my case i was running a local server, and working on a flutter app which was targeting Android, iOS, Web. This might help you. - Here
Android 模拟器在虚拟路由器后面运行,这会阻止直接访问本地计算机的网络接口。
要避免此问题,应使用本地计算机的 IP4 地址 (ipconfig /all) 启动 Web 服务器。然后,模拟器可以使用相同的 IP 地址而不是使用“localhost”访问服务器。
The Android emulator operates behind a virtual router, which prevents direct access to the network interface of the local computer.
To circumvent this issue, the web server should be started with the IP4 address of the local machine (ipconfig /all). Then, the emulator can access the server using the same IP address instead of using 'localhost'.
另一种解决方法是从 no-ip.org 获取免费域名并将其指向您的本地 IP 地址。
然后,您可以尝试
http://yourdomain.no-ip.org/yourwebservice
,而不是使用http://localhost/yourwebservice
Another workaround is to get a free domain from no-ip.org and point it to your local ip address.
Then, instead of using
http://localhost/yourwebservice
you can tryhttp://yourdomain.no-ip.org/yourwebservice
我知道这已经很旧了,但是如果您发现 10.0.2.2 不能作为计算机 IP,请按照 这些说明来找到它
I know this is old, but if you find that 10.0.2.2 is not working as the computer IP, follow these instructions to find it
本地主机指的是运行代码的设备,在本例中是模拟器。
如果您想引用运行 Android 模拟器的计算机,请改用 IP 地址 10.0.2.2。
您可以从此处阅读更多内容。
The localhost refers to the device on which the code is running, in this case the emulator.
If you want to refer to the computer which is running the Android simulator, use the IP address 10.0.2.2 instead.
You can read more from here.
使用
10.0.2.2
作为默认 AVD,使用10.0.3.2
作为Use
10.0.2.2
for default AVD and10.0.3.2
for genymotion.实际上,您可以使用
localhost:8000
连接到您计算机的本地主机,方法是每次运行模拟器时运行以下命令(仅在 Mac 上测试):只需将其放入 Android Studio 终端(查看 > 工具)窗口>终端)。
它基本上设置了一个反向代理,其中运行在您手机上的 http 服务器接受端口上的连接并将它们连接到您的计算机,反之亦然。
You can actually use
localhost:8000
to connect to your machine's localhost by running below command each time when you run your emulator (tested on Mac only):Just put it to Android Studio terminal (View > Tool Windows > Terminal).
It basically sets up a reverse proxy in which a http server running on your phone accepts connections on a port and wires them to your computer or vice versa.
我在家里的机器上成功使用了10.0.2.2,但是在工作中,它不起作用。经过几个小时的闲逛,我使用 Android 虚拟设备创建了一个新的模拟器实例 (AVD) 管理器,最后 10.0.2.2 工作了。
我不知道其他模拟器实例出了什么问题(平台相同),但如果您发现 10.0.2.2 不起作用,请尝试创建一个新的模拟器实例。
I used 10.0.2.2 successfully on my home machine, but at work, it did not work. After hours of fooling around, I created a new emulator instance using the Android Virtual Device (AVD) manager, and finally the 10.0.2.2 worked.
I don't know what was wrong with the other emulator instance (the platform was the same), but if you find 10.0.2.2 does not work, try creating a new emulator instance.
尝试
http://10.0.2.2:8080/
,其中8080
是您的端口号。它工作得很好。如果您只是尝试10.0.2.2
,它将无法工作。您需要为其添加端口号。另外,如果已安装 Microsoft IIS,请尝试从控制面板关闭该功能(如果使用任何 Windows 操作系统),然后按照上面的说明进行尝试。Try
http://10.0.2.2:8080/
where8080
is your port number. It worked perfectly. If you just try10.0.2.2
it won't work. You need to add port number to it. Also if Microsoft IIS has been installed try turning off that feature from control panel (if using any windows os) and then try as given above.根据文件:
检查 模拟器网络了解有关模拟器网络的更多技巧。
according to documentation:
check Emulator Networking for more tricks on emulator networking.