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.

Advertisements
 

Using Manifests in Scala to create a new instance of type T in a generic class February 5, 2011

Filed under: Scala — Rupert Bates @ 3:41 pm

To create a new instance of T in the class

class MyClass[T]

you can use the Manifest class as follows:

class MyClass[T](implicit man: Manifest[T]) {
  // get a new instance of T
  def getNewInstance =  man.erasure.newInstance
  // get Class[T]
  def getClassT = man.erasure.asInstanceOf[Class[T]]
}

What I initially didn’t get when I was trying to use this technique was how you can use it with a constructor which takes a non implicit argument. To do this you need the following syntax:

class MyClass[T](myConstructorParam : String)(implicit man: Manifest[T]) {
  // get a new instance of T
  def getNewInstance =  man.erasure.newInstance
  // get Class[T]
  def getClassT = man.erasure.asInstanceOf[Class[T]]
}