2

I'm using Retrofit+Gson for parsing JSON.

When I try parse response from Google Places API (ok, I don't try parse, I just try to make model for this response) and I get some error.

This is response from Google Place API:

    {
   "predictions" : [
      {
         "description" : "Николаевская область, Украина",
         "id" : "3bd747cc4efc2288da48942b909ce18a053c2060",
         "matched_substrings" : [
            {
               "length" : 5,
               "offset" : 0
            }
         ],
         "place_id" : "ChIJydRVsbqaxUARLq1R8Q3RgpM",
         "reference" : "ClRPAAAAwseWiG8NUMt7TqSqz9rMP8R2M4rX7-cMRmIp4OCYL-VdRSr5B5T_PMwWzYOydVStVpYDvm0ldXYPEzxFAuvn1LqhtWHdROhsERwvmx0tVlwSEFdMw0sOe3rDaB2AqKKmF-YaFLvhiEOz3Bklv5-iTa7QQORILVCU",
         "structured_formatting" : {
            "main_text" : "Николаевская область",
            "main_text_matched_substrings" : [
               {
                  "length" : 5,
                  "offset" : 0
               }
            ],
            "secondary_text" : "Украина"
         },
         "terms" : [
            {
               "offset" : 0,
               "value" : "Николаевская область"
            },
            {
               "offset" : 22,
               "value" : "Украина"
            }
         ],
         "types" : [ "administrative_area_level_1", "political", "geocode" ]
      }, ...],
   "status" : "OK"
}

This is my model for this response:

    public class GetGoogleMapPlacesResponse {
    @SerializedName("predictions")
    private List<GooglePlace> googlePlaces;

    public List<GooglePlace> getGooglePlaces() {
        return googlePlaces;
    }

    public void setGooglePlaces(List<GooglePlace> googlePlaces) {
        this.googlePlaces = googlePlaces;
    }
}

But when Retrofit try's to parse response to model I get error:

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.myapp.net.rest.response.GetGoogleMapPlacesResponse

And this is raw response in Debug mode: enter image description here

Artem
  • 4,569
  • 12
  • 44
  • 86
  • It might be helpful for you. http://stackoverflow.com/questions/22285661/gson-json-and-the-subtleties-of-linkedtreemap – Arkar Aung Feb 06 '17 at 15:44
  • i had this problem once, when i used a generics in models, that's not your case right? – mayosk Feb 06 '17 at 15:50
  • @mayosk no, in this requst I don't use generics – Artem Feb 06 '17 at 15:53
  • can you add constructor code of GetGoogleMapPlacesResponse? – mayosk Feb 06 '17 at 15:59
  • @Artem did you come across a solution to this. I too am stuck at the same place. – Hanu May 25 '17 at 11:16
  • @Amruta-Pani sorry, it did some months ago - try review your model in fields - for example in my case at this post - try commented googlePlaces list (your models) and look - is simple String or int field parse well by Gson or not - and try find error in your model (in my case GooglePlace). I opened project and looked at this code but didn't find answer - how I fix it((( sorry and good luck! – Artem May 25 '17 at 11:50
  • 1
    @Artem got it resolved by following this answer - https://stackoverflow.com/questions/32444863/google-gson-linkedtreemap-class-cast-to-myclass – Hanu May 26 '17 at 06:24

1 Answers1

1

You're missing a constructor of GetGoogleMapPlacesResponse model.

public class GetGoogleMapPlacesResponse {
     private List<GooglePlace> googlePlaces;
     private String status;

     public GetGoogleMapPlacesResponse(List<GooglePlace> googlePlaces, String status) {
         this.googlePlaces = googlePlaces;
         this.status = status;
     }

    ...getters & setters
}

But i highly suggest you to use AutoValue with Gson extension and then your model will look like this :

@AutoValue
public abstract class GetGoogleMapPlacesResponse {
      @SerializedName("predictions") public abstract List<GooglePlace> googlePlaces;
      public abstract String status;
}

For more info look here : https://github.com/rharter/auto-value-gson

mayosk
  • 603
  • 3
  • 14
  • first solution doesn't work for me, second - I think about this, but I dont want use mane libraries. – Artem Feb 06 '17 at 16:29
  • 1: paste method which cause error (how you access that predictions list) 2: just use it, immutability and readability is way to go – mayosk Feb 06 '17 at 16:38
  • @mayosk Gson does not require constructors to instantiate during deserialization. The issue the OP is facing with is broken type casting somewhere. – Lyubomyr Shaydariv Feb 06 '17 at 16:43
  • @LyubomyrShaydariv yes, but only if you use gson ^2.3.1 – mayosk Feb 06 '17 at 16:46
  • @mayosk Hm, the changelog says "No need to define no-args constructors for classes serialized with Gson" in v1.7... Am I missing something? – Lyubomyr Shaydariv Feb 06 '17 at 17:02