Wednesday, April 11, 2012

Internet not connecting in android emulator

Recently, while developing an Android Application, I was faced with a situation that wasted 2 hours of my time.  The issue was simple, my app was not able to access internet from the Android Emulator. 

Initially I thought fixing the issue should have been straight forward, but life is not always that simple. 

So what was the real issue because of which my app was not able to access internet?  There are multiple reasons because of which this issue could occur.  Hence, I decided to document my findings so that other could benefit from it.

There are two main symptoms of no internet connection on android emulator
  • Only your app is not able to access the internet
  • None of the apps are able to access the internet
Lets look at what are the reasons behind each of them.

Only your app is not able to access the internet

If only your app is not able to access the internet on the emulator, check if any other apps are able to access the internet or not.  For e.g. you could open up the browser application, visit  If the page opens up correctly then problem lies in your app itself and its simple to fix.

Basically, your app needs the Permission to access the internet.  This can be done by adding the following line just before the end of <manifest> tag inAndroidManifest.xml file of your application.
<uses-permission android:name="android.permission.INTERNET"/>

A sample AndroidManifest.xml file with the added permission would look like
<?xml version="1.0" encoding="utf-8"?>
    <application android:label="GitShahMobile"
        <activity android:name="GitShahActivity"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
    <uses-permission android:name="android.permission.INTERNET"/>

Compile and re-install the app in the emulator and try to access the internet from your app. It should work!

What is the use of uses-permission tag:

Android application can request certain permissions so that they can function properly.  Some examples of permissions are, get users location, make a call, access the internet etc. App has to explicitly specify this in the AndroidManifest.xml. 

When end users install such an app on their device, the android OS will notify the user that, app is requesting certain permissions.  If users are fine with that, then only the app will be installed.  Else users can deny the permission and the app will not be installed.

This mechanism is Androids way of implementing security and users privacy!

None of the apps are able to access the internet

Now this situation is tricky.  There are two reasons because of which this could happen
  • Proxy server is not configured on the emulator
  • Incorrect DNS used by the emulator

Configuring the proxy server on the emulator:

If you access internet via a proxy server, then you will need to configure the proxy server on the emulator as well.  This is done via the following steps.

Open up the Settings application on the Emulator 

Select the Wireless & Network settings
Wireless and Networks
Select the Mobile networks 
Mobile Networks
Select the Access Point Names
Access Point Names
Select the Access Point listed there.  In my case its Telkila
Select the access point
In the Edit access point section edit the value of Proxy and Port.  These should point to yourProxy server and Port that you use.
Set the proxy and port values
Thats it!  After doing these settings try to access the internet from the browser application on the emulator.  It should work!

If you are not behind a proxy (like me) then the problem is because of an incorrect dns server.  Lets look at how to fix the dns issue.

Incorrect DNS Used by the Emulator:

In my case none of the above things fixed the issue.  The real issue was that the DNS used by the Emulator was incorrect.  Fortunately there is a way in which we can specify which DNS server to use.  

Android Emulator takes a command line parameter -dns-server using which you can specify which DNS server to use.  The sample command to specify DNS server would look like
emulator.exe -avd MapsDevice2.2 -dns-server

-dns-server takes a comma separated list of IP address to use as DNS server.

That did the trick for me!  I was finally able to access internet from my app on the android emulator!

1 comment:

  1. how to set the proxy and mms option in my android emulator.