Thursday, January 31, 2013

Connecting an Android App to a Tomcat Localhost

While working on the development of an Android client for one of our web apps we ran into the task of connecting our client app to a java servlet running on a local instance of Tomcat under Eclipse.

It was then that we realized that the standard way to connect to localhost or didn't work with Android at all.

The reason is that both an AVD emulator and a real Android device will interpret the localhost as the machine which is running the app, that is, the Android emulator/device itself.

So, if you are trying to make your Android app to connect to a java servlet (or any web app) running on Tomcat under Eclipse, you must:

  1. change the IP part of the URL you're trying to connect to ( become for example) as explained in  the official Android documentation.
  2. if (1) for some reason it doesn't work, try setting the IP address of the machine which is running Eclipse as the IP part of the servlet URL; you can get it by running ipconfig under the start >> execute on Windows-based OSs.
  3. if (1) and (2) won't work, try to close and restart Eclipse.
  4. if none of the solutions above will work, man, it can only be a bug in your networking source code; debug it carefully and then try the previous steps again.

Just to be clear, this is only for testing purposes on localhost: restore the original URL address to connect to before to deploy your Android app or it won't work.

Have fun!


  1. The correct address for the Tomcat server is:

    Not !

  2. Thanks for your note Bob, it was a typo error! Now it's right and I added a link to the official Android documentation.