无法通过杰克逊

时间:2015-11-30 15:50:24

标签: android json jackson tumblr

好的,这件事让我疯了!

我收到了Tumblr的以下回复:

{
"meta": {
    "status": 200,
    "msg": "OK"
},
"response": {
    "blog": {
        "title": "heavy metal broke my \u2665",
        "name": "crawlingindark",
        "posts": 22362,
        "url": "http:\/\/crawlingindark.tumblr.com\/",
        "updated": 1447328080,
        "description": "\u003Ccenter\u003EI\u0027m \u003Ca href=\u0022http:\/\/crawlingindark.tumblr.com\/tagged\/me\u0022\u003ENeno\u003C\/a\u003E - 20 years old \n\u003Cp\u003Eand i love music\n\u003Cp\u003E.\n\u003Ccenter\u003E - m u l t i f a n d o m  -\n\u003Cp\u003E ..............................................\n\u003Cp\u003E  my \u003Ca href=\u0022http:\/\/crawlingindark.tumblr.com\/tagged\/my+edits\u0022\u003Eedits\u003C\/a\u003E \/ \u003Ca href=\u0022http:\/\/crawlingindark.tumblr.com\/tagged\/gifs\u0022\u003Egifs\u003C\/a\u003E",
        "is_nsfw": false,
        "ask": true,
        "ask_page_title": "Ask Shit",
        "ask_anon": true,
        "submission_page_title": "Submit",
        "share_likes": true,
        "likes": 14065
    },
    "posts": [{
        "blog_name": "crawlingindark",
        "id": 132110495244,
        "post_url": "http:\/\/crawlingindark.tumblr.com\/post\/132110495244",
        "slug": "",
        "type": "photo",
        "date": "2015-10-28 22:59:16 GMT",
        "timestamp": 1446073156,
        "state": "published",
        "format": "html",
        "reblog_key": "3PWT3Isv",
        "tags": [],
        "short_url": "http:\/\/tmblr.co\/ZlSQww1x2PYuC",
        "summary": "",
        "recommended_source": null,
        "recommended_color": null,
        "highlighted": [],
        "note_count": 319,
        "source_url": "http:\/\/paramass.tumblr.com\/post\/132100950816",
        "source_title": "paramass",
        "caption": "",
        "reblog": {
            "tree_html": "",
            "comment": ""
        },
        "trail": [],
        "photoset_layout": "11",
        "photos": [{
            "caption": "",
            "alt_sizes": [{
                "url": "https:\/\/40.media.tumblr.com\/d17078742be254f1ba709fa3a4f28dc5\/tumblr_nwritjzxzs1sdaok0o1_1280.png",
                "width": 800,
                "height": 600
            }, {
                "url": "https:\/\/41.media.tumblr.com\/d17078742be254f1ba709fa3a4f28dc5\/tumblr_nwritjzxzs1sdaok0o1_500.png",
                "width": 500,
                "height": 375
            }, {
                "url": "https:\/\/40.media.tumblr.com\/d17078742be254f1ba709fa3a4f28dc5\/tumblr_nwritjzxzs1sdaok0o1_400.png",
                "width": 400,
                "height": 300
            }, {
                "url": "https:\/\/40.media.tumblr.com\/d17078742be254f1ba709fa3a4f28dc5\/tumblr_nwritjzxzs1sdaok0o1_250.png",
                "width": 250,
                "height": 188
            }, {
                "url": "https:\/\/41.media.tumblr.com\/d17078742be254f1ba709fa3a4f28dc5\/tumblr_nwritjzxzs1sdaok0o1_100.png",
                "width": 100,
                "height": 75
            }, {
                "url": "https:\/\/36.media.tumblr.com\/d17078742be254f1ba709fa3a4f28dc5\/tumblr_nwritjzxzs1sdaok0o1_75sq.png",
                "width": 75,
                "height": 75
            }],
            "original_size": {
                "url": "https:\/\/40.media.tumblr.com\/d17078742be254f1ba709fa3a4f28dc5\/tumblr_nwritjzxzs1sdaok0o1_1280.png",
                "width": 800,
                "height": 600
            }
        }, {
            "caption": "",
            "alt_sizes": [{
                "url": "https:\/\/36.media.tumblr.com\/c63bb95138bd9fd935940405986e27b0\/tumblr_nwritjzxzs1sdaok0o2_1280.png",
                "width": 800,
                "height": 600
            }, {
                "url": "https:\/\/40.media.tumblr.com\/c63bb95138bd9fd935940405986e27b0\/tumblr_nwritjzxzs1sdaok0o2_500.png",
                "width": 500,
                "height": 375
            }, {
                "url": "https:\/\/40.media.tumblr.com\/c63bb95138bd9fd935940405986e27b0\/tumblr_nwritjzxzs1sdaok0o2_400.png",
                "width": 400,
                "height": 300
            }, {
                "url": "https:\/\/41.media.tumblr.com\/c63bb95138bd9fd935940405986e27b0\/tumblr_nwritjzxzs1sdaok0o2_250.png",
                "width": 250,
                "height": 188
            }, {
                "url": "https:\/\/41.media.tumblr.com\/c63bb95138bd9fd935940405986e27b0\/tumblr_nwritjzxzs1sdaok0o2_100.png",
                "width": 100,
                "height": 75
            }, {
                "url": "https:\/\/40.media.tumblr.com\/c63bb95138bd9fd935940405986e27b0\/tumblr_nwritjzxzs1sdaok0o2_75sq.png",
                "width": 75,
                "height": 75
            }],
            "original_size": {
                "url": "https:\/\/36.media.tumblr.com\/c63bb95138bd9fd935940405986e27b0\/tumblr_nwritjzxzs1sdaok0o2_1280.png",
                "width": 800,
                "height": 600
            }
        }]
    }, {
        "blog_name": "crawlingindark",
        "id": 132105216164,
        "post_url": "http:\/\/crawlingindark.tumblr.com\/post\/132105216164",
        "slug": "",
        "type": "photo",
        "date": "2015-10-28 21:29:22 GMT",
        "timestamp": 1446067762,
        "state": "published",
        "format": "html",
        "reblog_key": "aSVzZOdQ",
        "tags": [],
        "short_url": "http:\/\/tmblr.co\/ZlSQww1x25Q2a",
        "summary": "",
        "recommended_source": null,
        "recommended_color": null,
        "highlighted": [],
        "note_count": 223,
        "source_url": "http:\/\/paramass.tumblr.com\/post\/132037729414",
        "source_title": "paramass",
        "caption": "",
        "reblog": {
            "tree_html": "",
            "comment": ""
        },
        "trail": [],
        "image_permalink": "http:\/\/crawlingindark.tumblr.com\/image\/132105216164",
        "photos": [{
            "caption": "",
            "alt_sizes": [{
                "url": "https:\/\/41.media.tumblr.com\/e53498ccbe5fbd27605491dfe206117b\/tumblr_nwriitRseS1sdaok0o1_1280.png",
                "width": 667,
                "height": 934
            }, {
                "url": "https:\/\/36.media.tumblr.com\/e53498ccbe5fbd27605491dfe206117b\/tumblr_nwriitRseS1sdaok0o1_500.png",
                "width": 500,
                "height": 700
            }, {
                "url": "https:\/\/41.media.tumblr.com\/e53498ccbe5fbd27605491dfe206117b\/tumblr_nwriitRseS1sdaok0o1_400.png",
                "width": 400,
                "height": 560
            }, {
                "url": "https:\/\/41.media.tumblr.com\/e53498ccbe5fbd27605491dfe206117b\/tumblr_nwriitRseS1sdaok0o1_250.png",
                "width": 250,
                "height": 350
            }, {
                "url": "https:\/\/36.media.tumblr.com\/e53498ccbe5fbd27605491dfe206117b\/tumblr_nwriitRseS1sdaok0o1_100.png",
                "width": 100,
                "height": 140
            }, {
                "url": "https:\/\/40.media.tumblr.com\/e53498ccbe5fbd27605491dfe206117b\/tumblr_nwriitRseS1sdaok0o1_75sq.png",
                "width": 75,
                "height": 75
            }],
            "original_size": {
                "url": "https:\/\/41.media.tumblr.com\/e53498ccbe5fbd27605491dfe206117b\/tumblr_nwriitRseS1sdaok0o1_1280.png",
                "width": 667,
                "height": 934
            }
        }]
    }],
    "total_posts": 21670
}

}

这是对tumblr博客照片的回复。

然后我在http://www.jsonschema2pojo.org的帮助下为杰克逊生成了以下类别:

帖子:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "blog_name",
    "id",
    "post_url",
    "slug",
    "type",
    "date",
    "timestamp",
    "state",
    "format",
    "reblog_key",
    "tags",
    "short_url",
    "summary",
    "recommended_source",
    "recommended_color",
    "highlighted",
    "note_count",
    "source_url",
    "source_title",
    "caption",
    "reblog",
    "trail",
    "photoset_layout",
    "photos",
    "image_permalink"
})
public class Post {

    @JsonProperty("blog_name")
    private String blogName;
    @JsonProperty("id")
    private Long id;
    @JsonProperty("post_url")
    private String postUrl;
    @JsonProperty("slug")
    private String slug;
    @JsonProperty("type")
    private String type;
    @JsonProperty("date")
    private String date;
    @JsonProperty("timestamp")
    private Long timestamp;
    @JsonProperty("state")
    private String state;
    @JsonProperty("format")
    private String format;
    @JsonProperty("reblog_key")
    private String reblogKey;
    @JsonProperty("tags")
    private List<Object> tags = new ArrayList<Object>();
    @JsonProperty("short_url")
    private String shortUrl;
    @JsonProperty("summary")
    private String summary;
    @JsonProperty("recommended_source")
    private Object recommendedSource;
    @JsonProperty("recommended_color")
    private Object recommendedColor;
    @JsonProperty("highlighted")
    private List<Object> highlighted = new ArrayList<Object>();
    @JsonProperty("note_count")
    private Long noteCount;
    @JsonProperty("source_url")
    private String sourceUrl;
    @JsonProperty("source_title")
    private String sourceTitle;
    @JsonProperty("caption")
    private String caption;
    @JsonProperty("reblog")
    private se.onemanstudio.linkinpark.api.providers.photos.objects.tumblr.Reblog reblog;
    @JsonProperty("trail")
    private List<Object> trail = new ArrayList<Object>();
    @JsonProperty("photoset_layout")
    private String photosetLayout;
    @JsonProperty("photos")
    private List<Photo> photos = new ArrayList<Photo>();
    @JsonProperty("image_permalink")
    private String imagePermalink;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * No args constructor for use in serialization
     * 
     */
    public Post() {
    }

    /**
     * 
     * @param summary
     * @param sourceTitle
     * @param noteCount
     * @param state
     * @param highlighted
     * @param date
     * @param type
     * @param sourceUrl
     * @param recommendedColor
     * @param reblog
     * @param timestamp
     * @param id
     * @param blogName
     * @param trail
     * @param shortUrl
     * @param tags
     * @param photosetLayout
     * @param postUrl
     * @param caption
     * @param format
     * @param photos
     * @param reblogKey
     * @param imagePermalink
     * @param recommendedSource
     * @param slug
     */
    public Post(String blogName, Long id, String postUrl, String slug, String type, String date, Long timestamp, String state, String format, String reblogKey, List<Object> tags, String shortUrl, String summary, Object recommendedSource, Object recommendedColor, List<Object> highlighted, Long noteCount, String sourceUrl, String sourceTitle, String caption, se.onemanstudio.linkinpark.api.providers.photos.objects.tumblr.Reblog reblog, List<Object> trail, String photosetLayout, List<Photo> photos, String imagePermalink) {
        this.blogName = blogName;
        this.id = id;
        this.postUrl = postUrl;
        this.slug = slug;
        this.type = type;
        this.date = date;
        this.timestamp = timestamp;
        this.state = state;
        this.format = format;
        this.reblogKey = reblogKey;
        this.tags = tags;
        this.shortUrl = shortUrl;
        this.summary = summary;
        this.recommendedSource = recommendedSource;
        this.recommendedColor = recommendedColor;
        this.highlighted = highlighted;
        this.noteCount = noteCount;
        this.sourceUrl = sourceUrl;
        this.sourceTitle = sourceTitle;
        this.caption = caption;
        this.reblog = reblog;
        this.trail = trail;
        this.photosetLayout = photosetLayout;
        this.photos = photos;
        this.imagePermalink = imagePermalink;

        Toolkit.Logd("POST", "Created a post with source_url: " + this.sourceUrl + " and source_title: " + this.sourceTitle);
    }

    /**
     * 
     * @return
     *     The blogName
     */
    @JsonProperty("blog_name")
    public String getBlogName() {
        return blogName;
    }

    /**
     * 
     * @param blogName
     *     The blog_name
     */
    @JsonProperty("blog_name")
    public void setBlogName(String blogName) {
        this.blogName = blogName;
    }

    public Post withBlogName(String blogName) {
        this.blogName = blogName;
        return this;
    }

    /**
     * 
     * @return
     *     The id
     */
    @JsonProperty("id")
    public Long getId() {
        return id;
    }

    /**
     * 
     * @param id
     *     The id
     */
    @JsonProperty("id")
    public void setId(Long id) {
        this.id = id;
    }

    public Post withId(Long id) {
        this.id = id;
        return this;
    }

    /**
     * 
     * @return
     *     The postUrl
     */
    @JsonProperty("post_url")
    public String getPostUrl() {
        return postUrl;
    }

    /**
     * 
     * @param postUrl
     *     The post_url
     */
    @JsonProperty("post_url")
    public void setPostUrl(String postUrl) {
        this.postUrl = postUrl;
    }

    public Post withPostUrl(String postUrl) {
        this.postUrl = postUrl;
        return this;
    }

    /**
     * 
     * @return
     *     The photosetLayout
     */
    @JsonProperty("photoset_layout")
    public String getPhotosetLayout() {
        return photosetLayout;
    }

    /**
     * 
     * @param photosetLayout
     *     The photoset_layout
     */
    @JsonProperty("photoset_layout")
    public void setPhotosetLayout(String photosetLayout) {
        this.photosetLayout = photosetLayout;
    }

    public Post withPhotosetLayout(String photosetLayout) {
        this.photosetLayout = photosetLayout;
        return this;
    }

    /**
     * 
     * @return
     *     The photos
     */
    @JsonProperty("photos")
    public List<Photo> getPhotos() {
        return photos;
    }

    /**
     * 
     * @param photos
     *     The photos
     */
    @JsonProperty("photos")
    public void setPhotos(List<Photo> photos) {
        this.photos = photos;
    }

    public Post withPhotos(List<Photo> photos) {
        this.photos = photos;
        return this;
    }

    /**
     * 
     * @return
     *     The imagePermalink
     */
    @JsonProperty("image_permalink")
    public String getImagePermalink() {
        return imagePermalink;
    }

    /**
     * 
     * @param imagePermalink
     *     The image_permalink
     */
    @JsonProperty("image_permalink")
    public void setImagePermalink(String imagePermalink) {
        this.imagePermalink = imagePermalink;
    }

    public Post withImagePermalink(String imagePermalink) {
        this.imagePermalink = imagePermalink;
        return this;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    public Post withAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
        return this;
    }

}

和照片:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "caption",
    "alt_sizes",
    "original_size"
})
public class Photo {

    @JsonProperty("caption")
    private String caption;
    @JsonProperty("alt_sizes")
    private List<AltSize> altSizes = new ArrayList<AltSize>();
    @JsonProperty("original_size")
    private OriginalSize originalSize;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * No args constructor for use in serialization
     * 
     */
    public Photo() {
    }

    /**
     * 
     * @param originalSize
     * @param altSizes
     * @param caption
     */
    public Photo(String caption, List<AltSize> altSizes, OriginalSize originalSize) {
        this.caption = caption;
        this.altSizes = altSizes;
        this.originalSize = originalSize;
    }

    /**
     * 
     * @return
     *     The caption
     */
    @JsonProperty("caption")
    public String getCaption() {
        return caption;
    }

    /**
     * 
     * @param caption
     *     The caption
     */
    @JsonProperty("caption")
    public void setCaption(String caption) {
        this.caption = caption;
    }

    public Photo withCaption(String caption) {
        this.caption = caption;
        return this;
    }

    /**
     * 
     * @return
     *     The altSizes
     */
    @JsonProperty("alt_sizes")
    public List<AltSize> getAltSizes() {
        return altSizes;
    }

    /**
     * 
     * @param altSizes
     *     The alt_sizes
     */
    @JsonProperty("alt_sizes")
    public void setAltSizes(List<AltSize> altSizes) {
        this.altSizes = altSizes;
    }

    public Photo withAltSizes(List<AltSize> altSizes) {
        this.altSizes = altSizes;
        return this;
    }

    /**
     * 
     * @return
     *     The originalSize
     */
    @JsonProperty("original_size")
    public OriginalSize getOriginalSize() {
        return originalSize;
    }

    /**
     * 
     * @param originalSize
     *     The original_size
     */
    @JsonProperty("original_size")
    public void setOriginalSize(OriginalSize originalSize) {
        this.originalSize = originalSize;
    }

    public Photo withOriginalSize(OriginalSize originalSize) {
        this.originalSize = originalSize;
        return this;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    public Photo withAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
        return this;
    }

}

生成的OriginalSize.java文件如下:

@JsonInclude(JsonInclude.Include.NON_NULL)

@JsonPropertyOrder({
    "url",
    "width",
    "height"
})

public class OriginalSize {

    @JsonProperty("url")
    private String url;
    @JsonProperty("width")
    private Long width;
    @JsonProperty("height")
    private Long height;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * No args constructor for use in serialization
     * 
     */
    public OriginalSize() {
    }

    /**
     * 
     * @param height
     * @param width
     * @param url
     */
    public OriginalSize(String url, Long width, Long height) {
        this.url = url;
        this.width = width;
        this.height = height;
    }

    /**
     * 
     * @return
     *     The url
     */
    @JsonProperty("url")
    public String getUrl() {
        return url;
    }

    /**
     * 
     * @param url
     *     The url
     */
    @JsonProperty("url")
    public void setUrl(String url) {
        this.url = url;
    }

    public OriginalSize withUrl(String url) {
        this.url = url;
        return this;
    }

    /**
     * 
     * @return
     *     The width
     */
    @JsonProperty("width")
    public Long getWidth() {
        return width;
    }

    /**
     * 
     * @param width
     *     The width
     */
    @JsonProperty("width")
    public void setWidth(Long width) {
        this.width = width;
    }

    public OriginalSize withWidth(Long width) {
        this.width = width;
        return this;
    }

    /**
     * 
     * @return
     *     The height
     */
    @JsonProperty("height")
    public Long getHeight() {
        return height;
    }

    /**
     * 
     * @param height
     *     The height
     */
    @JsonProperty("height")
    public void setHeight(Long height) {
        this.height = height;
    }

    public OriginalSize withHeight(Long height) {
        this.height = height;
        return this;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    public OriginalSize withAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
        return this;
    }

}

生成的AltSize.java:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "url",
    "width",
    "height"
})
public class AltSize {

    @JsonProperty("url")
    private String url;
    @JsonProperty("width")
    private Long width;
    @JsonProperty("height")
    private Long height;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * No args constructor for use in serialization
     * 
     */
    public AltSize() {
    }

    /**
     * 
     * @param height
     * @param width
     * @param url
     */
    public AltSize(String url, Long width, Long height) {
        this.url = url;
        this.width = width;
        this.height = height;
    }

    /**
     * 
     * @return
     *     The url
     */
    @JsonProperty("url")
    public String getUrl() {
        return url;
    }

    /**
     * 
     * @param url
     *     The url
     */
    @JsonProperty("url")
    public void setUrl(String url) {
        this.url = url;
    }

    public AltSize withUrl(String url) {
        this.url = url;
        return this;
    }

    /**
     * 
     * @return
     *     The width
     */
    @JsonProperty("width")
    public Long getWidth() {
        return width;
    }

    /**
     * 
     * @param width
     *     The width
     */
    @JsonProperty("width")
    public void setWidth(Long width) {
        this.width = width;
    }

    public AltSize withWidth(Long width) {
        this.width = width;
        return this;
    }

    /**
     * 
     * @return
     *     The height
     */
    @JsonProperty("height")
    public Long getHeight() {
        return height;
    }

    /**
     * 
     * @param height
     *     The height
     */
    @JsonProperty("height")
    public void setHeight(Long height) {
        this.height = height;
    }

    public AltSize withHeight(Long height) {
        this.height = height;
        return this;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    public AltSize withAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
        return this;
    }

}

根据文件显示,一切似乎都很好。

然后我使用以下代码遍历内容:

public static ArrayList<GenericPhoto> extractPhotosFromTumblrContent(TumblrPhotoDataWrapper tumbrlData) {
    ArrayList<GenericPhoto> mPictures = new ArrayList<>();

Toolkit.Logd(TAG, "Working on the tumblr '" + tumbrlData.getSource() + "' which has " + tumbrlData.getTumblrRootContent().getResponse().getPosts().size() + " posts...");

for (Post post : tumbrlData.getTumblrRootContent().getResponse().getPosts()) {
    Toolkit.Logd(TAG, "The post with id " + post.getId() + " has " + post.getPhotos().size() + " photos");

    for (Photo photo : post.getPhotos()) {
        GenericPhoto wi = new GenericPhoto();

        wi.setPostId(post.getId());
        wi.setTimestamp(post.getTimestamp());

        //wi.setCaption(photo.getOriginalSize().getUrl());

        int totalAlternativeSizesOfPhotos = photo.getAltSizes().size();
        //wi.setUrlSmall(photo.getAltSizes().get(totalAlternativeSizesOfPhotos - 2).getUrl());
        //wi.setUrlLarge(photo.getOriginalSize().getUrl());
        //wi.setUrlSmall(photo.getAltSizes().get(2).getUrl());

        Toolkit.Logd(TAG, "The post id is " + post.getId() + " and it has " + totalAlternativeSizesOfPhotos + " alternative sizes");

        mPictures.add(wi);
    }
}

Toolkit.Logd(TAG, "I extracted " + mPictures.size() + " from the tumblr " + tumbrlData.getSource());

return mPictures;

}

只是浏览帖子的所有图片。

问题是即使映射正确地检测到每个帖子上有多少张照片(即2,5,8),它也会检测到0个替代尺寸并且没有照片的原始尺寸。就好像杰克逊完成所有工作时,每张照片的映射都不会发生。

我尝试使用jsonschema2pojo重新创建类,清理项目,清理缓存,但似乎没有任何工作。

可能是什么问题?我还应该在哪儿看? 可以与Proguard合作吗?可以在Android Studio中配置项目吗?

杰克逊在解析RSS新闻源以及YouTube的JSON响应方面完美无瑕地工作,这让我觉得Photos类或某些组件的配置有问题。

非常感谢任何帮助。

0 个答案:

没有答案
相关问题