简体   繁体   中英

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. Of course, my android mobile was recognized by adb and it connected my laptop via usb port.

I already read some similar questions at stackoverflow but still didn't know how to access webservice from a real android mobile. The answer of this question How can I access my localhost from my Android device? 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. 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).

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 ). 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. But if you instead see something like TCP [::127.0.0.1]:8888 (LISTEN) then you have identified your problem!

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 . 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.

In normal(default) settings, port 8888 throws error in WAMPSERVER (even in Windows Browser). That is why I explained the exact environment where port 8888 throws error.

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? Then you just need to connect to the wifi using your mobile and you should be able to access (192.168.xx.yyy)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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