Saturday, February 19, 2011

Android - Fixing the no internet connection issue on 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 http://news.google.com/.  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 in AndroidManifest.xml file of your application.

A sample AndroidManifest.xml file with the added permission would look like

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 
Settings

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 your Proxy 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

-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!

78 comments:

  1. You are a total genius!

    I wasted more than two hours trying to figure out why i could only access our 'safe sites' on the emulator's browser, and changing proxies in the eclipse option.
    Never did i think that it should be changed in the emulator device itself.
    thanks you.

    ReplyDelete
  2. Hi,

    I am glad the post helped you.

    Thanks & Regards

    Deep Shah

    ReplyDelete
  3. thanks, it helped me to find a correct direction to fix my issue.

    ReplyDelete
  4. c:\>emulator.exe -avd Android1.6 -dns-server 192.168.1.1
    DNS server '192.168.1.1' resolve to '192.168.1.1:55'
    ###warning: could not add DNS server to network stack
    ###warning: will use system default DNS server

    ReplyDelete
  5. Hello Jeet,

    There are multiple reasons because of which the warning messages you are seeing could appear. Try using the Googles public DNS server does this help?

    emulator.exe -avd Android1.6 -dns-server 8.8.8.8

    Also tell me which OS are you using? WindowsXP?

    ReplyDelete
  6. Hi,

    Worked for me by setting the DNS server to 8.8.8.8 and then taking care of setting the proxy host using an IP address instead of a name (otherwise the emulator will try to resolve your proxy's IP address using the public DNS, which will fail when your proxy is private)

    Thanks for the help

    ReplyDelete
  7. Hi Deep and Jeet, thanks I got the same issue and also wasted 2 hours of my time! But thanks to your help and I've set the DNS to 8.8.8.8 and it worked!

    ReplyDelete
  8. Dennis,

    Glad that it worked out for you.

    Verma,

    You are welcome.

    Thanks

    Deep Shah

    ReplyDelete
  9. I have been facing the same problem as mentioned...but in my case...the emulator goes to a not responding state...it crashes...i have tried the above solutions

    ReplyDelete
  10. I have also tried reinstalling android sdk eclipse and plugins

    ReplyDelete
  11. windows 7

    ReplyDelete
  12. Hello Avinash,

    Need more information on whats happening with your emulator. Does it crash when you open up your app?

    Does it crash on opening any application?

    Can you check if you can open up a browser and visit www.gmail.com on it?

    Try to narrow down the problem. Is the problem in your app or in the emulator/machine configuration?

    ReplyDelete
  13. Problem:
    Emulator crashes when i run my application which requires the connection to the internet...opening a browser it also crashes...ie not responding state...
    Solution:
    I reinstalled everything on a windows xp platform,it works fine now.

    After a few days tried it again on my laptop(Windows 7)setting it to the google 8.8.8.8 dns,it runs...so i have to do this everytime i start the emulator,then after making changes i run it from eclipse and it connects,thanks...

    ReplyDelete
  14. very nice post. i like how pretty much all possible problems are discussed and solved in once place.

    ReplyDelete
  15. Hello Brian,

    Glad that you liked the post.

    Thanks

    Deep Shah

    ReplyDelete
  16. Hi,
    I have a problem with my android application that it is supossused to connect to a remote interface of a jboss6 aplication server but when i try to do this:
    "
    try
    {

    initCtx = new InitialContext(env);
    x = initCtx.lookup("SessionRemoteAplicationLayer/remote");
    }
    "
    it crachs complitly.. I don't know what to do because I tryed everything that you suggest above and the internet start to work but the problem with my android application don't work.

    Can you please help me? Thanks a lot.

    Sorry about my english, i'm portuguese.

    ReplyDelete
  17. Hi Deep,

    I am facing an issue with the Emulator. I am trying to add a Google Account on the emulator (With Google SDK lvl 8 API) but it keeps giving me 'Could not establish a reliable connection to the server' error.

    I am able to browse from the emulator so there does not seem to be any issue with the network connection.

    I could very easily add a Google account to another emulator on another computer.

    Wondering if you have any clue about what could be going wrong?

    Regards

    ReplyDelete
  18. Hmm, I have not faced this issue before. But did some googling and found this link http://dottech.org/tipsntricks/18387/

    See if this helps you

    ReplyDelete
  19. Hello Carla Gregório,

    You cannot use InitialContext to lookup a remote service from Android code.

    From Android you can invoke the remote service over HTTP. We mostly return JSON as response from the HTTP Based RESTful service.

    Thanks

    Deep Shah

    ReplyDelete
  20. Hi Friends,

    in my system emulator is opend.while i opened the Xml file(in layout).emulator was skipped.what is the problem ?How can i rectify it?
    Thans,
    Nagesh

    ReplyDelete
  21. hello mr.shah

    im using ndk for my application in that it should work as server and client , as a client its working fine but while as server its not working.
    i did permission, dns server as 8.8.8.8 & port forwarding though its not working.

    -vignesh

    ReplyDelete
  22. hi friends ,

    i have doubt in android emulator weather emulator can run as server or not in my case its not running as server.can any one suggest it.

    ReplyDelete
  23. you rule.. I've been pulling my hair out trying to figure this out.. finally got a simple http page to download.. damn that was frustrating.. i'm pretty damn proficient in c#, java i understand well enough and can navigate for what i do not. Going nuts, kept thinking, it can't be this damn hard to download a file over the net.. it just can't.. you rule.. and some day when a bag of money falls out of the sky and lands on my head, my will will state you can have 3 hand fulls from it..

    ReplyDelete
  24. Hi Deep,
    Thank you for the great posting.

    My android emulator resolves dns successfully but the browser still saying google.com is not available

    [anna@centos5]./emulator -avd droid -dns-server 8.8.8.8
    DNS server name '8.8.8.8' resolved to 8.8.8.8:55

    I do have an internet connection and my browser work fine on centos5.

    Thanks,
    Anna

    ReplyDelete
  25. Looks like my issue is due to the old version of glibc in centos. My centos5 comes with GNU C Library version 2.5. Android emulator needs glibc 2.7. That must be it.

    Thanks,
    Anna

    ReplyDelete
  26. Thanks so much.
    Great job.Hats of to you.

    ReplyDelete
  27. That last solution did it for me. Thank you!

    ReplyDelete
  28. Hello anna,

    Glad that you found a fix. Its worth a blog. May be you should blog about the fix that you found.

    Thanks

    Deep Shah

    ReplyDelete
  29. Hello monkey,

    I totally wish that bag of money falls out of the sky and lands on my head :)

    Thanks

    Deep Shah

    ReplyDelete
  30. Hi: i think i found another issue. The -dns-server gets resolved to port 55, but my company dns servers listen on port 53. Is there a work around to get emulator to use 53 on dns?

    ReplyDelete
  31. Hi,

    I have been trying to access internet through my emulator, every time i open the browser i get the error saying there is no internet connection.
    I tried to follow the steps in your post but there was no options to enter proxy in APN menu.
    Please do suggest the method to overcome this..

    ReplyDelete
  32. I am using windows professional 7. I tried setting DNS server in comment line option
    like this
    -avd Android_2.0 -dns-server 10.67.62.50
    but getting error DNS server name '10.67.62.50' resolved to 10.67.62.50:55 . How can i fix this.

    ReplyDelete
  33. Thanks alot. I appreciate your blog. It worked for me. Cheers!!

    ReplyDelete
  34. Hello,

    Could you try with the public DNS server 8.8.8.8?

    Thanks

    Deep Shah

    ReplyDelete
  35. hi deep

    I am using ubuntu, i followed your steps, but i could not connect to the net, i am using internet through proxy server.

    Could plz tell me what could be cause?

    ReplyDelete
  36. hi Deep

    i try all that u say but i couldn't success.In my application i try to get information about wifi..Although my browser connect internet my application couldn't see any wireless information..myNetworkInfo.isConnected()return false value

    ReplyDelete
  37. Hello Deep,

    Not able to get my app to connect internet in Android4.0 inspite of adding permission in the manifest file. Tried starting the emulator by specifying the DNS server address but when I run the app I see the below message in my logcat when the line - httpConn.connect(); executes in the code, please note that httpConn is not null.

    LogCat shows Runtime error as - java.lang.RuntimeException: Unable to start activity ComponentInfo{net.learn2develop.HttpDownload/net.learn2develop.HttpDownload.HttpDownload}: java.lang.NullPointerException


    Thanks for your time.
    Amateur.

    ReplyDelete
  38. Hello cemile,

    Did add the permissions "ACCESS_WIFI_STATE" and "ACCESS_NETWORK_STATE" to the manifest?

    Thanks

    Deep Shah

    ReplyDelete
  39. Hello Deep,

    Amateur again here. I have tried almost everything. The google on my emulator works but app does not. I tried starting the emulator using google DNS and then installing app, tried my own DNS server IP. I believe my DNS has port 53 while emulator defaults to port 55.My home network does not have a proxy. I tried all after shutting off WINDOWS firewall of my Vista OS on laptop, however no luck. I have wasted 2 days on this issue without getting on the real work :(.

    Any suggestions?

    Thanks for your time.
    Amateur

    ReplyDelete
  40. Hi Deep;

    Yes i added this permissions to manifest.I run my application on mobile it worked properly..I guess emulator couldn't understand if computer connect internet via wireless or ethernet cable.am i right?

    Sincerely,
    Cemile

    ReplyDelete
  41. Hi Deep,
    I am also having the same problem that i am not getting internet on my emulator. I have tried it with the default browser and also with google maps. I am not behind a proxy. I am using a system with fedora 13 OS and am getting internet on my system .I am not running system with admin privilege. I was getting everything perfect and the connectivity suddenly failed one day. I think the issue may be due to DNS as you said.
    How can i fix this issue..Is it through terminal...?


    Sincereley,
    Ashish

    ReplyDelete
  42. Hey Deep!

    This is really awesome tips!

    I wasted my lot time to start internet in avd manager..But finally I used "emulator.exe -avd AndroidMap -dns-server 8.8.8.8" command in "Command Prompt" and start the internet and it was done...So great...

    Still, I have one problem, when I run application and start emulator in eclipse, while trying to use internet..I still get the error...

    I tried to solve it out by writing, "-avd Android -dns-server 8.8.8.8" and "-dns-server 8.8.8.8" in Eclipses in Window->Preferences->Android->Launch->"Default emulator Option" directory..

    I really have an emergency to solve out this problem. So, if you know where the problem is, then please provide me solution soon....

    Thanks a lot in advance...

    ReplyDelete
  43. This comment has been removed by the author.

    ReplyDelete
  44. Great! Thank you for this very useful information :)

    ReplyDelete
  45. Genius!!! THANKS!!!!

    ReplyDelete
  46. Hi,

    I am developing a SIP protocol based Android application which is having calling features over VOIP.

    I am able to use internet connection when i run that application in emulator but when i run the same application in my samsung galaxy phone then its not able to access internet in it.

    I have already put below line in my application's manifest.xml file


    But still i am facing issue in connecting internet from my phone.
    Note: I am already using SIP based other applications in my same phone, which is able to access internet and functionality also works fine, its just the issue with my developed application.

    Can you please suggest me a way to fix it out?

    Thanks

    ReplyDelete
  47. Dude...You saved lot of my time buddy!!! Thanks a lot!!

    ReplyDelete
  48. Hello Sir,

    I am a newbie in Android development, i am not able to connect to internet after using the below commands, i get the following output

    C:\Program Files\Android\android-sdk\tools>emulator.exe -avd Demodevices -dns-se
    rver 8.8.8.8

    C:\Program Files\Android\android-sdk\tools>DNS server name '8.8.8.8' resolved to
    8.8.8.8:55
    emulator: WARNING: Unable to create sensors port: Unknown error

    i have also included the proxy server and port details of mine as mentioned by you. Kindly help, i am trying to fix this issue for more then a month now.

    ReplyDelete
  49. thanks after a long struggle a find the solution thanks a lot my dear

    ReplyDelete
  50. Hello Deep,
    I am not able hit the internet/intranet through the native apps installed on my android simulator .I have tried all the above options you a provided , still the issue exists. Please note the simulator is installed in VM machine and have all the privileges .please help me in troubleshooting the issue.

    Thanks & Regards
    Pravin

    ReplyDelete
  51. Hey Deep

    You blog is very informative
    However I tried -
    emulator -avd emulatorname -dns-server 10.155.21.206
    I got the error
    C:\Program Files\Android\android-sdk\tools>DNS server name '10.155.21.206' resolved to 10.155.21.206:55
    emulator: WARNING: Unable to create sensors port: Unknown error
    I tried 8.8.8.8 also. That didnt work either.gave me a similar error. Im using a script to connect to the internet in internet explorer but no proxy. Could company policies have something to do with this error?

    ReplyDelete
  52. when editing the Androidmanifest, do you use notepad or command prompt?

    ReplyDelete
  53. where can I find the Androidmanifest.xml file? HELP!!

    ReplyDelete
  54. The manifest file (Androidmanifest.xml) is in the root folder of your app-project thus you can find your project files at your android working space directory then you can select the project you want to edit its manifest then good to go, and heey, XML can b edited by using any editor but why suffering find other editors you can still use eclipse to edit any XML file

    ReplyDelete
  55. I just want to know if you are not behind any proxy server, do you still have to provide those username and password, and where do I get them!!

    ReplyDelete
  56. Thanks man. That was very helpful. It did the trick for me. I've been beating my head against the desk all day trying all kinds of scenarios. The whole time all it needed was the dns-server to be set from CLI. - Now, the next thing I need to do is figure out why in the heck the emulator crashes when I attempt to fill in a name field (example... name field for an eMail account, or APN name, or just about any other name field [except for the owner info box, doesn't seem to crash on boxes, just single line fields]).

    ReplyDelete
  57. The trick by me was to use the comand line to execute the emulator, and setting the IP address for the -dns-server x.x.x.x where x is the IP address to my router.
    Thank you very much
    Another question, i am able to access the internet now, but why is the Add account under Accounts & Sync not working?
    Appreciate any help.
    Manny E.

    ReplyDelete
  58. Never faced any such problem before but will keep this in mind if incase in future this type of error arises.

    ReplyDelete
  59. Hi Deep...Thanks for the solution..:) it works perfectly..But everytime i close the emulator and run it again, the apn changes are deleted... and i have to do it again...Its really a headache doing it everytime..Any Solution????
    Thank you,
    Jay

    ReplyDelete
  60. Hii deep same problem arises with me...I tried with the above command lines but it doesn't work....emulator.exe -avd MapsDevice2.2 -dns-server 192.168.1.1 i used emulator.exe -avd api9 -dns-server 8.8.8.8.....
    can u please help me....

    ReplyDelete
    Replies
    1. I am getting message like this Unknown savevm section type 95

      Delete
  61. thank you da , emulator.exe -avd And_2.2 -dns-server 8.8.8.8, works for me

    ReplyDelete
  62. i have tried all the thing you have said ,and my emulator browser work fine but nothing else worked,even map is also does not work plz help ihave already wasted 3 day.

    ReplyDelete
  63. Having a similar problem. Unfortunately the fixes didn't solve it for me.
    Situation: Testing an app for a friend. App needs to connect online to update server. Fails to establish connection.
    Have asked friend if she did the permission-thing. Yup, it is there. Tried the DNS. Same result. Tried setting a proxy. Same result. Tried with and without antivirus enabled.
    Frankly, I'm stumped. The browser on the emulator CAN connect to the net. Just the apps can not.
    Any ideas would be welcome as I have hit a brick wall with this.
    Thanks

    ReplyDelete
  64. Sir please help me out... I am a beginner to android. When i am giving the command emulator.exe -avd<> -dns-server<>.. it says emulator.exe is not recognized as an internal command.. what am i supposed to do...????
    please help me asap...

    ReplyDelete
  65. Good post, thanx loads

    ReplyDelete
  66. Hi!

    I have a problem. Browser not connected to internet.
    No proxy
    -dns-server 8.8.8.8 (I tried also with another)

    not response.

    Antivir offline
    Win7 firewall offline

    Tested with Telkila ..no internet
    I need this for me?
    Deleted Telkila..no internet

    Eclipse reinstall: done
    AVD reinstall: done
    Android SDK reinstall: done

    Browser not connected...why?

    Any idea?

    Thx

    ReplyDelete
  67. D:\Android\android-sdk\tools>emulator.exe -avd avd2.1 -dns-server 8.8.8.8

    D:\Android\android-sdk\tools>DNS server name '8.8.8.8' resolved to 8.8.8.8:55
    Failed to create Context 0x3005
    emulator: WARNING: Could not initialize OpenglES emulation, using software renderer.
    what to do i tried everything -dns-server8.8.8.8 and 8.8.4.4...
    what to do
    please reply fast its urgent

    ReplyDelete
  68. You guy save my day.. Thanks from Peru!!

    ReplyDelete
  69. Can't we set Additional Emulator Command line Parameters before Running the Android Application on Emulator. This is available Under Target Tab in Run configuration of the Android Application.

    we can set "-dns-server ,"

    ReplyDelete
  70. thanks a lot man it worked!!
    Thaks ton..

    ReplyDelete
  71. The following worked for me.
    emulator -avd {NameofEmu} -partition-size 500 -no-audio -no-boot-anim -dns-server 0.0.0.0

    ReplyDelete
  72. Genius!! it worked!! Thanks!

    ReplyDelete
  73. hello i develop android app,in this app i send my data on myhost,
    when i post data using httppost then i got error java.net.UnknowHostException www.myhost.com,

    i try all of this, Internet permission, apn setting dns setting, so on.

    any solution...?

    ReplyDelete
  74. Thanxx bro...it worked. :)

    I have done one thing instead of steps mentioned in "Incorrect DNS Used by the Emulator:" topic above.

    I went to eclipse>>Window>>Preference>>Android>>Launch

    within 'Default emulator option' i specified "-dns-server 8.8.8.8,8.8.4.4"

    its seems to be alternative part instead of doing command stuff...is it??

    but thanxxx other details worked for me..:)

    ReplyDelete
  75. I have been reading this but I still cant get where I'm supposed to change the sever number to 8.8.8.8 please give a step by step explanation

    ReplyDelete

Have some Fun!