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) {
  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.


Getting Json.Net and Gson json libraries to play nicely with dates using ISO formatting January 28, 2011

Filed under: C#,Java,Web,WebServices — Rupert Bates @ 2:38 pm

I am working on something at the moment where I have a service written in .net which serves json created using the Json.Net library. This is then consumed by a client app written in Java which uses the Gson library to deserialize that json into Java classes. This was all working fine except for dates which caused parse errors in Gson.

The solution I am using is:

use ISO time format when serializing with Json.Net

    string json = JsonConvert.SerializeObject(entry, new IsoDateTimeConverter());    

When Deserializing with Gson set the date format as follows:

        GsonBuilder b = new GsonBuilder();
        Gson gson = b.create();
        gson.fromJson(json, MyObjectType.class);