1

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
  • 171
  • 1
  • 2
  • 9
  • 1
    "Can someone tell me what that is?" -- 127.0.0.1. "And can I have them both run off of 1 URL" -- I have no idea what that means. – CommonsWare Feb 15 '14 at 17:20
  • 1
    you need to use IP of you PC ... it should work in both cases ... – Selvin Feb 15 '14 at 17:23
  • When I do `127.0.0.1` I get a connection refused error. My second part of the question is. If I want to be able to use the emulator and my actual device, 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.@CommonsWare – Anonymous Feb 15 '14 at 17:23
  • I use `ifconfig` and get `127.0.0.1` and get `Connection to http://127.0.0.1:8080 refused. In my logcat @Selvin – Anonymous Feb 15 '14 at 17:25
  • 1
    does your PC is connected to any network? – Selvin Feb 15 '14 at 17:28
  • My computer is connected to a network. – Anonymous Feb 15 '14 at 17:29

3 Answers3

6

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 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 – Anonymous Feb 15 '14 at 17:50
  • 1
    @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. – CommonsWare Feb 15 '14 at 18:08
  • 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 – Anonymous Feb 15 '14 at 18:21
  • 1
    @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 http://serverfault.com. – CommonsWare Feb 15 '14 at 18:41
  • Alright. Thank you. I'll hopefully figure it out. :) – Anonymous Feb 15 '14 at 18:45
2

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

kta
  • 19,412
  • 7
  • 65
  • 47
1

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.

nobalG
  • 4,544
  • 3
  • 34
  • 72
  • @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 – nobalG Apr 22 '15 at 14:52