Connecting to localhost web server from android application

Dec 12

Written by: Goran Siric
12/12/2011 9:24 PM  RssIcon

To allow your application to connect to the internet first you need to do is to modify AndroidManifest.xml file to request INTERNET permission for your application.

To do this open AndroidManifest.xml file and add following line if not exists:

<uses-permission android:name="android.permission.INTERNET"/>

To connect to your local web server (localhost), you must use IP address of  your computer instead of localhost keyword or IP address. That's it because IP address is internally used by android emulator or your android device. 

Somewhere I found that you can use IP adress to connect to  your local web server. I tried it but this not work for me. I think that this depends of type of web server you have installed.

If you want to know more about connecting to the Internet from Android application you can read my blog entry: Android application and the Internet

4 comment(s) so far...


Great Info.

Great information you got here. I've been reading about this topic for one week now for my papers in school and thank God I found it here in your blog. I had a great time reading this.

By Caverta on   2/20/2012 12:26 PM

Re: Connecting to localhost web server from android application

I can't connect to your entry Android application and the Internet

By anoop on   7/8/2012 12:36 PM


I saw your entry& thanks for ur info

By anoop on   7/8/2012 1:10 PM

Re: Connecting to localhost web server from android application

The correct IP address to use to talk to the host running the emulator is I've abstracted communication with a webserver in my app behind this little nugget:

public static boolean isEmulator()
if (isDebug == null)
isDebug = "google_sdk".equals(Build.PRODUCT) ||
return isDebug;

public static String getServerUrl()
return (isEmulator() ? Constants.SERVER_URL_TEST : Constants.SERVER_URL_PUBLIC);

By Brian Krahmer on   11/26/2012 3:56 AM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 

Search Blog

If you found this useful,
you can buy me a coffe :)
By me a coffe through PayPal :)

Featured articles

Android "DataEntry ListView" Design Pattern

Have you realized the importance of ListView control in developing android applications ?

GIMP script for creating Android icons at once

Script for creating Android icons for different screen resolutions at once. Icons can be saved using standard Android icons naming conventions and saved in appropriate folders.

Creating Android button with image and text using relative layout

Source code with examples how to use relative layout to create nice buttons with text and images in Android

Android application and the Internet

Tutorial about connecting to the web pages on the Internet from Android application, using both POST and GET web requests.

Recent Entries

Comments about Parking SMS Scheduler
Comments about cities and parking zones
Android "DataEntry ListView" Design Pattern
Android application and the Internet
Keep my DNN site alive
Android default icons

Recent Comments

Re: Comments about Parking SMS Scheduler
Great app!
It would be very useful to me if support for dual sim phones is added, i.e. choice of sending SMS from secondary or primary sim card.
Re: How to check if your android application is running in debug or release mode ?
Such a nice article. Exactly the thing I was looking for. Thank you very much Goran Siric.
Re: Creating Android button with image and text using relative layout
In code I am using:

Button bt = new Button(context);

to set drawable background for button. This way button looks like standard android button.
You can call setBackgroundDrawable() in code to change current button drawable(states).
If you want to set background drawable in xml resource files, then you can sliglty change code to not use this two lines of code if background drawable is allready set.
Re: Creating Android button with image and text using relative layout
awesome example and easy to follow. how do I set button states such as on button pressed, button focused and change the background color?
I like your post a lot! You should write some more on this!Great job coming with such terrific post!