Thứ Sáu, 25 tháng 5, 2012

Java.Net.UnknownHostException On Android – A List Of Possible Causes


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 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:
    1System.setProperty("http.proxyHost""my.proxyhost.com");
    2System.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 :
    1try {
    2InetAddress address = InetAddress.getByName(Url);
    3catch (UnknownHostException e) {
    4e.printStackTrace();
    5}
  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.

Không có nhận xét nào: