Android Localhost?

Anonymous picture Anonymous · Feb 15, 2014 · Viewed 14.7k times · Source

I know this has been asked and answered before, but I use 10.0.2.2 for the emulator. And I heard I must use something else for my real device. Can someone tell me what that is? And can I have them both run off of 1 URL. Instead of having one for the emulator and actual device.

Answer

CommonsWare picture CommonsWare · Feb 15, 2014

Android Localhost?

localhost of an Android device is localhost, 127.0.0.1.

I use 10.0.2.2 for the emulator

That refers to localhost of the computer running the emulator.

And I heard I must use something else for my real device

You need to use the IP address of the computer, on whatever network that the computer and the device share. You cannot access localhost on one machine from another machine, by definition.

Of course, you also need to figure whatever it is that is running on that computer to listen to more than its own localhost.

Is there a way I can use a single IP Instead of using 10.0.2.2 for my emulator and having to use another one for my device

Not really. Moreover, neither of the values that you are thinking of are the right ones for a release build of your app, in all likelihood. There, you will need to use the domain name (or, in a pinch, IP address) of some publicly-visible server that everyone's devices can reach.