1

First of all, i'm using retrofit with gson and autovalue-gson extension

Check here : https://github.com/rharter/auto-value-gson

In API i have a different models, where API returns json with id, description etc and also array of messages, but these messages may vary depending on what type of social media i want to (facebook, twitter, instagram etc...). So i made model like this :

@AutoValue
public abstract class SocialStream<T> {
  public abstract int id();
  ......
  ......

  public abstract List<T> messages();

  public static <T> TypeAdapter<SocialStream<T>> typeAdapter(Gson gson, TypeToken<? extends SocialStream<T>> typeToken) {
    return new AutoValue_SocialStream.GsonTypeAdapter(gson, typeToken);
  }
}

As you can see i also passed typetoken to AutoValue typeAdapter, and everything seems to work, i already logged whole json, but problem occurs when i try to use it as my model.

SocialStream<FacebookPost> post = getList().get(0);

method getList() returns array of these messages and this is the line where i'm getting exception:

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example.model.FacebookPost
at 
Bhadresh Kathiriya
  • 3,147
  • 2
  • 21
  • 41
mayosk
  • 603
  • 3
  • 14

1 Answers1

0

I have exactly the same problem like me. The key is creating a customized deserializer as Gson has no idea about the real class inside. try this https://stackoverflow.com/a/34685986/2458190

Community
  • 1
  • 1
aaronhigh
  • 91
  • 3