Accessing Localhost of Pc from Usb Connected Android Mobile Device

Accessing localhost of PC from USB connected Android mobile 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:

  1. first connect your real device with the USB cable (make sure you can upload the app on your device)
  2. get the IP address from the device you connect, which starts with 192.168.x.x:port
  3. open the "Network and Sharing Center"
  4. click on the "Local Area Connection" from the device and choose "Details"
  5. copy the "IPv4 address" to your app and replace it like: http://192.168.x.x:port/test.php
  6. upload your app (again) to your real device
  7. go to properties and turn "USB tethering" on
  8. run your application on the device

It should now work.

How can I access my localhost from my Android device?

USB doesn't provide network to mobile device.

If both your desktop and phone are connected to the same WiFi (or any other local network), then use your desktop IP address assigned by the router (not localhost and not 127.0.0.1).

To find out the IP address of your desktop:

  • type into the command line ipconfig (Windows) or ifconfig (Unix)

    • on Linux the one-liner ifconfig | grep "inet " | grep -v 127.0.0.1 will yield only the important stuff
    • there's a bunch of suggestions on how to have a similar output on Windows
  • there's going to be a bunch of IP's
  • try all of them (except the forementioned localhost and 127.0.0.1)

If your phone is connected to the mobile network, then things are going to be harder.

Either go hardcore:

  • first find out your router external IP address (https://www.google.de/search?q=myip)
  • then, on the router, forward some port to <your desktop IP>:<server port number>
  • finally use the external IP address and forwarded port

Otherwise use something like xip.io or ngrok.

NOTE: The ifconfig command has been deprecated and thus missing by default on Debian Linux, starting from Debian stretch. The new and recommended alternative for examining a network configuration on Debian Linux is ip command. For example to use ip command to display a network configuration run the following:

ip address

The above ip command can be abbreviated to:

ip a

If you still prefer to use ifconfig as part of your daily sys admin routine, you can easily install it as part of the net-tools package.

apt-get install net-tools

Reference is here

how to access localhost on android device

USB doesn't provide network to mobile device. If it's connected to your wifi, then hit your laptop address provided by the router. If it's connected to your mobile network, then first find out your router external IP address, then forward some port to that 10.0.2.2:portno and finally you'll be able to see that server from your device.

How can I access my localhost from my Android device?

USB doesn't provide network to mobile device.

If both your desktop and phone are connected to the same WiFi (or any other local network), then use your desktop IP address assigned by the router (not localhost and not 127.0.0.1).

To find out the IP address of your desktop:

  • type into the command line ipconfig (Windows) or ifconfig (Unix)

    • on Linux the one-liner ifconfig | grep "inet " | grep -v 127.0.0.1 will yield only the important stuff
    • there's a bunch of suggestions on how to have a similar output on Windows
  • there's going to be a bunch of IP's
  • try all of them (except the forementioned localhost and 127.0.0.1)

If your phone is connected to the mobile network, then things are going to be harder.

Either go hardcore:

  • first find out your router external IP address (https://www.google.de/search?q=myip)
  • then, on the router, forward some port to <your desktop IP>:<server port number>
  • finally use the external IP address and forwarded port

Otherwise use something like xip.io or ngrok.

NOTE: The ifconfig command has been deprecated and thus missing by default on Debian Linux, starting from Debian stretch. The new and recommended alternative for examining a network configuration on Debian Linux is ip command. For example to use ip command to display a network configuration run the following:

ip address

The above ip command can be abbreviated to:

ip a

If you still prefer to use ifconfig as part of your daily sys admin routine, you can easily install it as part of the net-tools package.

apt-get install net-tools

Reference is here

How to browse localhost on Android device?

I use my local ip for that i.e. 192.168.0.1 and it works.

android connect to PC's localhost when debugger on mobile device

As 10.0.2.2 is your system (pc)'s local host address (from emulator only). Actually android doesn't recognized localhost in url. so 10.0.2.2 is for that meant. Also for android device loopback address is 127.0.0.1.

Your url with 10.0.2.2 is correct. Also you can use Static IP of your system.

Just check for

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

in your application's manifest file.

EDIT:

Here you are using port 8080 so, try with adb command on your host machine.

adb forward tcp:8080 tcp:8080

Also please elaborate on this line "i want to debugger my application on my mobile".

Note:

If you are going to test on real device use your Network IP of system (PC).



Related Topics



Leave a reply



Submit