Android Localhost?
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.
Anonymous
Updated on June 17, 2022Comments
-
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 of1
URL. Instead of having one for the emulator and actual device. -
Anonymous over 10 yearsAlright, thanks for being super informative. My server is running on
127.0.0.1:8080
and in other wordslocalhost
. Here is theBASE_URL
of what I currently havehttp://10.0.2.2:8080/wwwroot/api/
. So I doifconfig
and get the ip address127.0.0.1
. When is use that I get a connection refused error in the log cat -
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 over 10 yearsCan 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 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 over 10 yearsAlright. Thank you. I'll hopefully figure it out. :)
-
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