Juri StrumpflohnerFollowJuri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops. Juri is also a recognized Google Developer Expert in Web Technologies
Accessing the host machine from your Android emulator
2 min read
I recently started a project where I develop an Android app (there will some posts later on ;) ) which accesses a remote server as its back-end, hosted on the Google Appengine.
When you develop, you normally start your Appengine server locally from within Eclipse (this has been very nicely configured by the Google Eclipse plugin wizard) s.t. it runs on localhost:8080. However when you try to access "http://localhost:8080" from your Android app, you will get a nice "Connection refused" exception. This is because localhost is the phone itself, basically the emulator.
After posting on SO, some gave me the hint of starting the appengine with you public IP address. This can be done in the Eclipse Run/Debug configuration under the "(x)=" arguments tab by passing the "--address="
Fair enough, but this requires you to change the IP address in the run configuration every time you switch network + in the code (preferably in some properties file) where you access your webserver of course. Moreover when you develop in a team this is quite cumbersome.
There is however a far better solution. Thanks to a hint from a SO post, you can access your host machine with the IP address "10.0.2.2". This has been designed in this way by the Android team. So your webserver can perfectly run at localhost and from your Android app you can access it via "http://10.0.2.2:8080".
Questions? Thoughts? Hit me up on Twitter