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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…