I'm able to access my laptop web server using the Android emulator, I'm using 10.0.2.2:portno
works well.
But when I connect my real Android phone, the phone browser can't connect to the same web server on my laptop. The phone is connected to the laptop using a USB cable. If I run the adb devices command, I can see my phone.
What am I missing?
its very simple , - GOTO command line (Window + R [type cmd]) - type ipconfig , that will show the current IP address of your PC - write taht IP address on your Android Phone's browser with :80 e.g (http://192.168.x.x:80)
Done
localhost will appear on your Phone
Was running into this problem using several different localhost servers. Finally got my app up and running on the phone in seconds just by using the Python simple server. It only takes a few seconds so is worth a try before getting into any more complicated solutions. First, make sure you have Python installed.
cmd+r
and typepython
for Windows or$ python --version
in mac terminal.Next:
Then just find the address of your host machine on the network, I used
System Preferences/Sharing
on mac to find it. Tap that into your Android device and should load yourindex.html
and you should be good.If not then the problem is something else and you may want to look into some of the other suggested solutions. Good luck!
* EDIT *
Another quick solution to try if you're using Chrome is the Web Server for Chrome extension. I found it a quick and super easy way to get access to localhost on my phone. Just make sure to check
Accessible to local network
under theOptions
and it should work on your cell without any problem.What worked for me ( target: debug an windows server app running on localhost:99999 from an client app running on android phone )
then the service app url (my_dev_machine_ip:99999/path_to_service) was accessible from the phone.
Personally I do not insist to use localhost when using device, there is no easy solution.
Just start the activity where you connect to localhost where you can use emulator. Whatever info you have to get from device can be generated with ease and can be sent to activity as parameter.
It is very simple.
Turn on Wifi Hotspot of your Android phone/router and connect your Laptop to your phone.
Start your server at localhost (I am using wamp server)
Now open command prompt and enter ipconfig command you will get following things
Copy this 192.168.43.76 in your mobile browser.
Note : Please set your network as "Home Network". Setting Home Network means allowing your PC to share stuff with other devices on the same network. If your using Windows 10 go to WiFi > Network properties of current network > Make this PC Discoverable.
Use this in your ubuntu/Macbook to get the ip address of your system. Your mobile and system should be in the same network
ip addr | grep inet
This will give you an ip address which looks like192.168.168.46
. Using this in your smartphone.Hope this helps.