ChatGPT解决这个技术问题 Extra ChatGPT

How to browse localhost on Android device?

I try to browse localhost on my HTC Magic. I have connected my device with Eclipse via USB. When browsing http://10.0.2.2 I get "Page not available". I remember, some days ago it worked.

But on the emulator I am able to browse localhost

Any ideas?

10.0.2.2:[portNumber] ...........
Your question is/was wrong. If you want to connect from one device to another it has nothing to do with "localhost" (and therefore obviously not with 10.0.2.2 - the alias to the loopback interface ONLY for the emulator).

R
Ryan M

Easier way to check is in browser of emulator type 10.0.2.2 instead of localhost.


F
Fedor

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


but actually 192.168.0.1 is pointing to the emulators/devices localhost. I have a webservice running on my computers localhost(apache) and I want it to access on my device
well, i remeber that when I connect my device to my local wlan network it is able to request localhost via 10.0.2.2, via usb I think it makes no sence to do networking^^
It's work!!! I'm just pointing to my gatway, e.g, 192.168.10.1. Thanks!
佚名

to access localhost on emulator: 10.0.2.2. However this may not always work for example if you have something other than a web server such as XMPP server.

assuming that you're on the same wireless network...

find your local ip (should be something 192.168.1.x) - go to the command line and type 'ipconfig' to get this. where x is the assigned local ip of the server machine. use your local ip for your android device to connect to your localhost.

it worked for me.


"However this may not always work" It always does! Of course you have to specify the port on which the respective service is listening.
M
Michael Denham

If you want to access a server running on your PC from your Android device via your wireless network, first run the command ipconfig on your PC (use run (Windows logo + R), cmd, ipconfig).

Note the IPv4 address: (it should be 192.168.0.x) for some x. Use this as the server IP address, together with the port number, e.g. 192.168.0.7:8080, in your code.

Your Android device will then access the server via your wireless network router.


"it should be 192.168.0.x". No it shouldn't. It may be but doesn't have to. You just have to choose the correct adapter (wireless lan).
D
Dimitris

I needed to see localhost on my android device as well (Samsung S3) as I was developing a Java Web-application.
By far the fastest and easiest way is to go to this link and follow instructions: https://developer.chrome.com/devtools/docs/remote-debugging

* Note: You have to use Google Chrome.* My summary of the above link:

Connect PC with Phone over USB.

Turn on Phone's "Developer options" from Settings

Go to about:inspect URL in PC's browser

Check "Discover USB Devices" (forward Ports if you are using them in your web-application)

Copy/paste localhost required link to text field in browser and click Open.

Piece of cake


S
Shobhit Puri

You can get a public URL for your server running on a specific port on localhost.

At my work place I could access the local server by using the local IP address of my machine in the app, as most of the other answers suggest. But at home I wasn't able to do that for some reason. After trying many answers and spending many hours, I came across https://ngrok.com. It is pretty straight forward. Just download it from within the folder do:

ngrok portnumber

( from command prompt in windows)

./ngrok portnumber

(from terminal in linux)

This will give you a public URL for your local server running on that port number on localhost. You can include in your app and debug it using that URL.

You can securely expose a local web server to the internet and capture all traffic for detailed inspection. You can share the URL with your colleague developer also who might be working remotely and can debug the App/Server interaction.

Hope this saves someone's time someday.


R
Rob

Combining a few of the answers above plus one other additional item solved the problem for me.

As mentioned above, turn your firewall off [add a specific rule allowing the incoming connections to the port once you've successfully connected] Find you IP address via ipconfig as mentioned above Verify that your webserver is binding to the ip address found above and not just 127.0.0.1. Test this locally by browsing to the ip address and port. E.g. 192.168.1.113:8888. If it's not, find a solution. E.g. https://groups.google.com/forum/?fromgroups=#!topic/google-appengine-java/z4rtqkKO2hg Now test it out on your Android device. Note that I also turned off my data connection and exclusively used a wifi connection on my Android.


H
Haroldo Gondim

Mac OSX Users

If your phone and laptop are on the same wifi:

Go to System Preferences > Network to obtain your IP address

On your mobile browser, type [your IP address]:3000 to access localhost:3000

e.g. 12.45.123.456:3000


m
makt

If your localhost is not running on the default HTTP port(which is port 80), you need to specify the port in your url to something that corresponds to the port on which your localhost is running. E.g. If your localhost is running on, say port 85, Your url should be

http://10.0.2.2:85

Y
Young

For the mac user:

I have worked on this problem for one afternoon until I realized the Xampp I used was not the real "Xampp" It was Xampp VM which runs itself based on a Linux virtual machine. That made it not running on localhost, instead, another IP. I installed the real Xampp and run my local server on localhost and then just access it with the IP of my mac.

Hope this will help someone.


J
Junuxx

If your firewall is on, turn it off and use IPv4 to test your app in the actual device, then test your application.


D
Deepika Rajani

I had similar issue but I could not resolve it using static ip address or changing firewall settings. I found a useful utility which can be configured in a minute.

We can host our local web server on cloud for free. On exposing it on cloud we get a different URL which we can use instead of localhost and access the webserver from anywhere.

The utility is ngrok https://ngrok.com/download Steps:

Signup Download Extract the file and double click to run it, this will open a command prompt Type "ngrok.exe http 80" without quotes to host for example XAMPP apache server which runs on port 80. Copy the new url name generated on the cmd prompt for e.g. if it is like this "fafb42f.ngrok.io"

URL like : http://localhost/php/test.php Should be modified like this : http://fafb42f.ngrok.io/php/test.php

Now this URL can be accessed from phone.


e
edwin

I use testproxy to do this.

npm install testproxy

testproxy http://10.0.2.2

You then get the url (and QR code) you can access on your mobile device. It even works with virtual machines you can't reach by just entering the IP of your dev machine.


y
yshahak

I used ngrok but now it need registration and it also has a connections request limit. Now I'm using LocalTunnel and so far it's much better.