Of Code and Me

Somewhere to write down all the stuff I'm going to forget and then need

java.net.UnknownHostException on Android – a list of possible causes February 22, 2011

Filed under: Android,Error,WebServices — Rupert Bates @ 3:54 pm

This is a list of possible solutions to the error:

     java.net.UnknownHostException: myapp.co.uk  at java.net.InetAddress.lookupHostByName(InetAddress.java:513)

when trying to access a website on Android.

They run from most to least likely, start at the top and work your way down:

  1. Check that you have <uses-permission android:name=”android.permission.INTERNET” /> set in your AndroidManifest.xml file (on the same level as application tag)
  2. If you are behind a proxy you may need to do the following:
    System.setProperty("http.proxyHost", "my.proxyhost.com");
    System.setProperty("http.proxyPort", "1234");
    
  3. If you are using an emulator try deleting and recreating the virtual device.
  4. If you are on a real device try switching the wifi on and then off again
  5. If it is something which happens once when an emulator or device boots but is ok later you could try ‘warming up’ the dns by running code such as the following before your own web request :
    try {
    InetAddress address = InetAddress.getByName(Url);
    } catch (UnknownHostException e) {
    e.printStackTrace();
    }
    
  6. Try rebooting your development machine – I’ve heard of this working

If none of the above work and you find another solution then let me know.

 

10 Responses to “java.net.UnknownHostException on Android – a list of possible causes”

  1. S.J.Fuhry Says:

    Make sure you aren’t trying to pass the http:// prefix. Silly.

    • James Says:

      I spent hours tearing my hair out. I tried your solution and it worked. First words out of my mouth when it worked: “YOU GOTTA BE #@$%^#@ KIDDING ME!”

      Thanks for sharing this. =)

  2. Andrey_rus Says:

    There is one more possible solution.

    When i have tested my application on real device locally, i have created domain “test” and passed as Url. But you MUST define as domain of second level, for example “test.zz” (there must be dot).

  3. carlos Says:

    oh my gosh! i have been struggeling on uploading a picture to my server and changing the manifest was the solution. thanks for your help thanks a lot !

  4. antonio Says:

    Rebooting and creating a new emulator worked for me.. Mine is working with the prefix “http” …

  5. ALEJANDRA Says:

    Buenas tardes, no se si ustedes me puedan ayudar o al menos me podrian indicar que debo hacer, y es lo siguente mi cuenta de facebook me aparecio inhabilitada por un phishing no se que sera eso y deseo habilitarla,les agradesco su respuesta ,

  6. Upendrakumar Says:

    How to get my confrmtion code

  7. maged918 Says:

    Number 5 worked for me 🙂 Thanks!


Leave a reply to antonio Cancel reply