Accessing localhost of PC from USB connected Android mobile device
I have an android device (Samsung galaxy tab) connected to my PC via USB .
I want to use WebServices and run a web page which is located on my local xampp server of my PC on my android device .
I can achieve it if I have both , my PC and android device , connected on the same wireless network .
However is it possible to access the localhost page without any wireless connection , using only USB connection .
(PS : This same scenario works when I run the app on an emulator and use the ip 10.0.2.2 to access the localhost on my PC)
Google posted a solution for this kind of problem here.
The steps:
- Connect your Android device and your development machine with USB debugging enabled
- Open Chrome in your development machine, open new tab, right click in the new browser tab, click inspect
- Click the three dots icon on right top side , -> More Tools, Remote Devices.
- Look at bottom of the screen, make sure your device name is appeared on the list with Green colored dot.
- Look below at the settings part, check the Port forwarding mark
- Add rule. Example, if your python web server is running on your machine localhost:5000 and you want to access it from your device port 3333, you type
3333
on the left part, and typelocalhost:5000
, and click add rule. - Voila, now you can access your web server from your device. Try open new browser tab, and visit http://localhost:3333 from your device
I finally solved this problem. I used Samsung Galaxy S with Froyo. The "port" below is the same port what you use for the emulator (10.0.2.2:port). What I did:
- first connect your real device with the USB cable (make sure you can upload the app on your device)
- get the IP address from the device you connect, which starts with 192.168.x.x:port
- open the "Network and Sharing Center"
- click on the "Local Area Connection" from the device and choose "Details"
- copy the "IPv4 address" to your app and replace it like:
http://192.168.x.x:port/test.php
- upload your app (again) to your real device
- go to properties and turn "USB tethering" on
- run your application on the device
It should now work.