开发者

GSON and the Flickr API problems: JsonParseException when deserializing json

开发者 https://www.devze.com 2023-01-05 01:00 出处:网络
I\'m relatively new to java and android development so sorry if this is a silly question. I\'m building simple app, simply to learn the android ropes, that will display a list of the most recent photo

I'm relatively new to java and android development so sorry if this is a silly question. I'm building simple app, simply to learn the android ropes, that will display a list of the most recent photos uploaded to flickr. The app is constructed but it's crapping out when it tries to deserialize the json that I get back from flickr. Here is my error:

06-23 21:11:25.560: WARN/System.err(298): com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@43e28168 failed to deserialized json object {"page":1,"pages":100,"perpage":10,"total":1000,"photo":[{"id":"4728507833","owner":"59492791@N00","secret":"496b96968d","server":"1407","farm":2,"title":"DSCN2733","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1407/4728507833_496b96968d_s.jpg","height_sq":75,"width_sq":75},{"id":"4728507883","owner":"30728507@N07","secret":"ab80a5bdf4","server":"1194","farm":2,"title":"Sultan","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1194/4728507883_ab80a5bdf4_s.jpg","height_sq":75,"width_sq":75},{"id":"4728507955","owner":"30311414@N00","secret":"7d6ea1cd08","server":"1377","farm":2,"title":"DSC04455","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1377/4728507955_7d6ea1cd08_s.jpg","height_sq":75,"width_sq":75},{"id":"4728507979","owner":"24737003@N04","secret":"c76cfe4afb","server":"1162","farm":2,"title":"IMGP1429","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1162/4728507979_c76cfe4afb_s.jpg","height_sq":75,"width_sq":75},{"id":"4728508009","owner":"62084706@N00","secret":"76b858efe7","server":"1366","farm":2,"title":"DSC_2459.JPG","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1366/4728508009_76b858efe7_开发者_运维知识库s.jpg","height_sq":75,"width_sq":75},{"id":"4729153856","owner":"50879555@N02","secret":"843d4f56ec","server":"1129","farm":2,"title":"IMG_1585","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1129/4729153856_843d4f56ec_s.jpg","height_sq":75,"width_sq":75},{"id":"4729153942","owner":"43941986@N08","secret":"f7ea2d3037","server":"1257","farm":2,"title":"IMG_1126","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1257/4729153942_f7ea2d3037_s.jpg","height_sq":75,"width_sq":75},{"id":"4729153952","owner":"50634775@N07","secret":"af6d937dcd","server":"1129","farm":2,"title":"069_69","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1129/4729153952_af6d937dcd_s.jpg","height_sq":75,"width_sq":75},{"id":"4729153962","owner":"34771042@N07","secret":"71f6c321eb","server":"1014","farm":2,"title":"Snapshot_010","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1014/4729153962_71f6c321eb_s.jpg","height_sq":75,"width_sq":75},{"id":"4729153964","owner":"14318596@N08","secret":"6ab627b565","server":"1364","farm":2,"title":"DSC_0679","ispublic":1,"isfriend":0,"isfamily":0,"url_sq":"http://farm2.static.flickr.com/1364/4729153964_6ab627b565_s.jpg","height_sq":75,"width_sq":75}]} given the type com.google.gson.ParameterizedTypeImpl@3dd03b7
06-23 21:11:25.580: WARN/System.err(298):     at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:63)
06-23 21:11:25.580: WARN/System.err(298):     at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
06-23 21:11:25.600: WARN/System.err(298):     at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:117)
06-23 21:11:25.600: WARN/System.err(298):     at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:150)
06-23 21:11:25.610: WARN/System.err(298):     at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:123)
...
06-23 21:11:25.851: WARN/System.err(298): Caused by: java.lang.IllegalStateException: This is not a JSON Array.
06-23 21:11:25.901: WARN/System.err(298):     at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:100)
06-23 21:11:25.911: WARN/System.err(298):     at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTypeAdapters.java:468)
06-23 21:11:25.921: WARN/System.err(298):     at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTypeAdapters.java:435)
06-23 21:11:25.921: WARN/System.err(298):     at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:50)
06-23 21:11:25.931: WARN/System.err(298):     ... 26 more

From, the error I'm assuming it has something GSON doesn't like about the json that is being returned but I don't know enough about how java converts objects and other types of variables during the whole operation.

I'm not sure if you will need this but here is some of the code from the project:

String response = client.getResponse();
response = response.replace("jsonFlickrApi(", "");
response = response.substring(0,response.length()-2);
Gson gson = new Gson();

FlickrPhotos flickrphotos = new FlickrPhotos();
try {
    flickrphotos = gson.fromJson(response, FlickrPhotos.class);
} catch(Exception e) {
    e.printStackTrace();
}

Here is FlickrPhotos:

public class FlickrPhotos {
    private String page, pages, perpage, total;
    private List<FlickrPhoto> photos;

    public List<FlickrPhoto> getPhotos() {
        return photos;
    }
    // More getters
}

Here is FlickrPhoto:

public class FlickrPhoto {
    private String id, owner, secret, server, title, width_sq, url_sq;
    // getters
}

Any ideas? Thanks so much for your time and probably patience...(If I'm noobing it up!)


One thing that stood out to me by just looking was this photo vs photos discrepancy:

... "total":1000,"photo":[{ ...

vs

private List<FlickrPhoto> photos;

public List<FlickrPhoto> getPhotos() {
    return photos;
}

If you haven't already, maybe try renaming your List to just photo instead?

Also, many of the properties (page, pages, perpage, width_sq etc) aren't Strings in the JSON. I would think there's an automatic type converter that deals with that, but you might as well declare those as ints.

0

精彩评论

暂无评论...
验证码 换一张
取 消