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:
- 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.
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) orifconfig
(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
- on Linux the one-liner
- there's going to be a bunch of IP's
- try all of them (except the forementioned
localhost
and127.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) orifconfig
(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
- on Linux the one-liner
- there's going to be a bunch of IP's
- try all of them (except the forementioned
localhost
and127.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
How to Open a Second Activity on Click of Button in Android App
How to Put Multiple Project_Number/Sender Id in Google-Services.JSON
How to Play Ringtone/Alarm Sound in Android
Programmatically Select Item Listview in Android
How to Send Parameters from a Notification-Click to an Activity
How to Create a File in Android
Override Home and Back Button Is Case a Boolean Is True
Android Location Providers - Gps or Network Provider
Android: How to Use Onbackpressed() with Toast
Android Heap Size on Different Phones/Devices and Os Versions
Recommended Way/Order to Read Data from a Webservice, Parse That Data and Insert It in a SQLite Db
How to Handle an Asynctask During Screen Rotation
How to Send a Sms Using Smsmanager in Dual Sim Mobile
How to Style an Android Switch
In Eclipse, Unable to Reference an Android Library Project in Another Android Project