Android Localhost?

15,475

Solution 1

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.

Solution 2

You have to use the ip address of your computer for a real device.check your ip address and try to connect with it.In my case I came to know this when I was trying to access some webapp from my android device and my web application was on tomcat container on my pc.It worked very well for me.

Solution 3

Get the ip address by running ipconfig from dos prompt and use it.

Share:
15,475
Anonymous
Author by

Anonymous

Updated on June 17, 2022

Comments

  • Anonymous
    Anonymous almost 2 years

    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.

  • Anonymous
    Anonymous over 10 years
    Alright, thanks for being super informative. My server is running on 127.0.0.1:8080 and in other words localhost. Here is the BASE_URL of what I currently have http://10.0.2.2:8080/wwwroot/api/. So I do ifconfig and get the ip address 127.0.0.1. When is use that I get a connection refused error in the log cat
  • CommonsWare
    CommonsWare over 10 years
    @user3311121: "My server is running on 127.0.0.1:8080" -- there is your problem. Fix that. Have your server listen to 127.0.0.1:8080 and :8080 on an IP address visible from the device.
  • Anonymous
    Anonymous over 10 years
    Can you please elaborate a bit? So I'd have my server listen to 127.0.0.1:8080? And :8080:? I'm not exactly sure what that means @CommonsWare
  • CommonsWare
    CommonsWare over 10 years
    @user3311121: What is the IP address of your computer, other than localhost? You claim that it is connected to a network, which means the computer has an IP address to participate on that network. You need to configure your server to listen to THAT IP ADDRESS in addition to (or possibly instead of) localhost. If you need further assistance in learning how to configure a Web server in this fashion, please visit serverfault.com.
  • Anonymous
    Anonymous over 10 years
    Alright. Thank you. I'll hopefully figure it out. :)
  • nobalG
    nobalG about 9 years
    @green You need to specify the Ip address of the machine where you have deployed your web app or WebService...and do specify the reason for DOWNVOTE