有没有一种简单的方法来设置android模拟器来访问主机的局域网?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3010130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there an easy way to setup the android emulator to access the LAN of the host machine?
提问by Longfield
I would like to access a web service provided by one of the machines on my LAN from the android emulator.
我想从 android 模拟器访问 LAN 上其中一台机器提供的 Web 服务。
If the service was running on the same machine where the emulator is running (called host), I could add a network redirection and access the 10.0.2.2 (host loopback interface in the emulator) with the correct port.
如果服务在运行模拟器的同一台机器上运行(称为主机),我可以添加网络重定向并使用正确的端口访问 10.0.2.2(模拟器中的主机环回接口)。
However it is running on another machine on the LAN. I guess I could add another redirection on the host additionally to the above one (would have to fight with iptables though ... ), but does a more simple solution exist ?
但是,它正在 LAN 上的另一台机器上运行。我想我可以在主机上添加另一个重定向到上述重定向(尽管必须与 iptables 斗争......),但是是否存在更简单的解决方案?
采纳答案by Longfield
Well, it just works (TM).
好吧,它只是有效(TM)。
In the android emulator manual that I had read before testing (I try to RTFM before doing things) this is stated:
在我在测试之前阅读的 android 模拟器手册中(我在做事之前尝试 RTFM),这是说明:
Each instance of the emulator runs behind a virtual router/firewall service that isolates it from your development machine's network interfaces and settings and from the internet. An emulated device can not see your development machine or other emulator instances on the network. Instead, it sees only that it is connected through Ethernet to a router/firewall.
模拟器的每个实例都在一个虚拟路由器/防火墙服务后面运行,该服务将它与开发机器的网络接口和设置以及 Internet 隔离开来。仿真设备无法在网络上看到您的开发机器或其他仿真器实例。相反,它只看到它通过以太网连接到路由器/防火墙。
This made me think that it could not access my LAN. I was wrong, just adding the IP of the server on my LAN allowed me to access it from the emulator.
这让我认为它无法访问我的局域网。我错了,只是在我的 LAN 上添加服务器的 IP 允许我从模拟器访问它。
回答by tumbudu
As we know, emulator can access host machine by IP 10.0.2.2, so I tried IP forwarding but I could not make it working.. :|
众所周知,模拟器可以通过 IP 10.0.2.2 访问主机,所以我尝试了 IP 转发,但无法使其正常工作..:|
I followed link below for forwarding request from localhost(10.0.2.2 in case of emulator) to the other machine in LAN.
我按照下面的链接将请求从 localhost(在模拟器的情况下为 10.0.2.2)转发到 LAN 中的另一台机器。
http://www.debuntu.org/how-to-redirecting-network-traffic-a-new-ip-using-iptables
http://www.debuntu.org/how-to-redirecting-network-traffic-a-new-ip-using-iptables
回答by user1859022
as of 2018-08it seems the "it just works (TM)" certificate has been revoked
截至2018 年 8 月,“it just works (TM)”证书似乎已被吊销
here is what I needed to access a server in my local network:
这是我访问本地网络中的服务器所需的内容:
host ip (my pc): 192.168.15.102/24
server ip: 192.168.200.33/24
create a port forwarding rule in an elevated command prompt on hostusing netsh
在主机上使用提升的命令提示符创建端口转发规则netsh
netsh interface portproxy add v4tov4 listenport=80 connectport=80 connectaddress=192.168.200.33
start the mobile device emulator and browse to 10.0.2.2(as mentioned in the documentation):
启动移动设备模拟器并浏览到10.0.2.2(如文档中所述):
10.0.2.2 Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)
10.0.2.2 主机环回接口的特殊别名(即开发机器上的 127.0.0.1)
回答by mtmurdock
You could get an actual android device and connect it to the LAN (i have done this).
你可以得到一个真正的安卓设备并将它连接到局域网(我已经这样做了)。
How are you connecting to the host machine? through html? because if your app requires a hard line connection to the machine then its not a very good solution for a mobiledevice.
你是如何连接到主机的?通过html?因为如果您的应用程序需要与机器的硬线连接,那么它对于移动设备来说不是一个很好的解决方案。