Android – Accessing localhost of PC from USB connected Android mobile device

android

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)

Best Solution

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 three dots, -> 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 type localhost: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