3

I have a WCF Service that returns a List of objects to an Android app. One of the properties is a DateTime property. The return format is JSON and I am getting the date in this format /Date(1441117247253+0200)/ on the Android side. I am using com.squareup.retrofit:retrofit:1.9.0 to get the data from my service.

I have no clue how to use Retrofit to create a the string date into a Date object. I had a look at this http://square.github.io/retrofit/ under the Custom Converters section, but do not know how to go further. This is what I've tried but I do not know how to implement the converter class.

creating the restAdapter like so:

restAdapter = new RestAdapter.Builder()
                .setConverter(new DotNetDateConverter())
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(API).build();

and the DotNetDateConverter Class which I do not know how to implement further:

public class DotNetDateConverter implements Converter
{
    @Override
    public Object fromBody(TypedInput body, Type type) throws ConversionException
    {
        return null;
    }

    @Override
    public TypedOutput toBody(Object object)
    {
        return null;
    }
}

there are other fields in the response which are fine, but how do I change the /Date(1441117247253+0200)/ to a proper java.util.Date object? Without the converter I get com.google.gson.JsonSyntaxException:/Date(1441117247253+0200)/ obviously because the string cannot be converter to a date.

Any assistance would greatly be appreciated.

Community
  • 1
  • 1
pjdupreez
  • 659
  • 1
  • 8
  • 19
  • One option is to change your property data type from DateTime to String type on the WCF side and convert the value to string (i.e.: to convert to short date use dateTimeProperty.ToString("d")). And your service operation response should have the correct data format in JSON like "9/2/2015". – Leo Nix Sep 02 '15 at 20:44
  • yeah, i thought about that, but then I'll be changing my service to accommodate one client type. the client should really adapt to the service, if you know what I mean. – pjdupreez Sep 02 '15 at 23:03
  • Make two objects on the service, one that returns DateTime, another that returns client specific DateTime `dateTimeProperty.ToString("client-supported-format")`. Otherwise are you going to write a low level DateTime converter in javascript just to support the service when you already have the control over it? I wont do that. – DJ' Sep 03 '15 at 09:11

1 Answers1

3

Ok, so after some digging around, I came across this. My approach was wrong. So I ended up creating a Converter like this:

public class DotNetDateConverter implements JsonDeserializer<Date>
{
    @Override
    public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
    {
        String s = json.getAsJsonPrimitive().getAsString();
        long l = Long.parseLong(s.substring(s.indexOf("(")+1, s.indexOf("+")));
        Date d = new Date(l);
        return d;
    }
}

I also had to register it like so:

GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(Date.class, new DotNetDateConverter());

        restAdapter = new RestAdapter.Builder()
                .setConverter(new GsonConverter(gsonBuilder.create()))
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(API).build();

I had to change the code a bit to accommodate my specific scenario, that being dates come in as /Date(1441117247253+0200)/ with the time zone. Maybe someone else might find some use for this...

Community
  • 1
  • 1
pjdupreez
  • 659
  • 1
  • 8
  • 19