简体   繁体   English

无法从Android设备访问localhost

[英]Cannot access localhost from Android device

Emulator can access web service fromlocal host but my real device cannot although I use LAN ip address (192.168.xx.yyy) instead of 10.0.2.2. 模拟器可以从本地主机访问Web服务,但我的真实设备不能使用LAN IP地址(192.168.xx.yyy)而不是10.0.2.2。 Of course, my android mobile was recognized by adb and it connected my laptop via usb port. 当然,我的Android手机被adb认可,它通过USB端口连接我的笔记本电脑。

I already read some similar questions at stackoverflow but still didn't know how to access webservice from a real android mobile. 我已经在stackoverflow上阅读了一些类似的问题,但仍然不知道如何从真正的Android手机访问webservice。 The answer of this question How can I access my localhost from my Android device? 这个问题的答案如何从我的Android设备访问我的localhost? my be useful but i don't really understand that answer. 我很有用,但我真的不明白这个答案。

Anyone helps me? 有人帮我吗? Thanks you! 谢谢!

Possibly the web server is listening on the loopback interface but not on the network interface. Web服务器可能正在侦听环回接口,但不在网络接口上侦听。 This means that hits on 127.0.0.1 and localhost will work, but 192.168.xxx.xxx will not (whether from localhost, LAN, or WAN). 这意味着127.0.0.1localhost上的命中将起作用,但192.168.xxx.xxx将不起作用(无论是来自localhost,LAN还是WAN)。

To determine which interface the server is listening on, look here for a command to tell you about the listening ports (I used lsof -Pan -i tcp -i udp ). 要确定服务器正在侦听哪个接口,请在此处查看告诉您有关侦听端口的命令(我使用了lsof -Pan -i tcp -i udp )。 Then look for your server process in the list. 然后在列表中查找您的服务器进程。 For the lsof command, if for port 8888 you see something like TCP *:8888 (LISTEN) then your server is listening on all interfaces. 对于lsof命令,如果对于端口8888,您会看到类似TCP *:8888 (LISTEN)那么您的服务器正在侦听所有接口。 But if you instead see something like TCP [::127.0.0.1]:8888 (LISTEN) then you have identified your problem! 但是如果你看到像TCP [::127.0.0.1]:8888 (LISTEN)这样的东西,那么你已经发现了你的问题!

The next step to solve your problem is to set up your server's run configuration to listen on all interfaces. 解决问题的下一步是设置服务器的运行配置以监听所有接口。 I don't know what server you are using, but if you can specify an IP address, then you may want to try 0.0.0.0 . 我不知道您使用的服务器,但如果您可以指定IP地址,那么您可能需要尝试0.0.0.0 Usually you can do this near where it lets you specify the listening port. 通常,您可以在允许指定侦听端口的位置附近执行此操作。 Thus, if you have a configuration like: 因此,如果您有如下配置:

--port 8888

Then you can try: 然后你可以尝试:

--port 8888 --address 0.0.0.0

In my opinion, the below point is more important for developer community 在我看来,以下几点对于开发者社区来说更为重要

I am not a network expert; 我不是网络专家; like me many developers may be there. 像我一样,许多开发人员可能会在那

change to 0.0.0.0:80 in httpd.conf is very important. 在httpd.conf中更改为0.0.0.0:80非常重要。

In normal(default) settings, port 8888 throws error in WAMPSERVER (even in Windows Browser). 在正常(默认)设置中,端口8888在WAMPSERVER中引发错误(即使在Windows浏览器中)。 That is why I explained the exact environment where port 8888 throws error. 这就是我解释端口8888抛出错误的确切环境的原因。

Specific information about what configuration throws error and what configuration works successfully is more important. 有关哪些配置抛出错误以及哪些配置成功运行的具体信息更为重要。

Have you tried using a wifi router which is connected to your LAN? 您是否尝试使用连接到LAN的wifi路由器? Then you just need to connect to the wifi using your mobile and you should be able to access (192.168.xx.yyy) 然后你只需要使用你的手机连接到wifi,你应该能够访问(192.168.xx.yyy)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM