如何在retofit库中获得这种类型的respose?

时间:2017-08-26 14:57:03

标签: android

{
    "status": "ok",
    "count": 2,
    "pages": 1,
    "category": {
        "id": 5,
        "slug": "campus",
        "title": "Campus",
        "description": "",
        "parent": 0,
        "post_count": 2
    },
    "posts": [
        {
            "id": 42,
            "type": "post",

            "url": "http://jantasamachar.com/changing-rajya-sabha-presents-modi-with-unique-opportunity-to-unleash-reforms/",
            "status": "publish",
            "title": "Changing Rajya Sabha presents Modi with unique opportunity to unleash reforms",

            "content":
 "date": "2017-08-02 07:00:28",
            "modified": "2017-08-02 07:10:48",
            "categories": [
                {
                    "id": 5,
                    "slug": "campus",
                    "title": "Campus",
                    "description": "",
                    "parent": 0,
                    "post_count": 2
                }
            ],
            "tags": [],
            "author": {
                "id": 1,
                "slug": "admin",
                "name": "admin",
                "first_name": "",
                "last_name": "",
                "nickname": "admin",
                "url": "",
                "description": ""
            },
            "comments": [],
            "attachments": [],
            "comment_count": 0,
            "comment_status": "open",

            "custom_fields": {
                "post_media_position": [
                    "media_full_width"
                ],
                "homepage_slider_add": [
                    "1"
                ],
                "featured_post_add": [
                    "1"
                ],
                "category_slider_add": [
                    "1"
                ],
                "post_sidebar": [
                    "post_sidebar_on"
                ],
                "enable_rating": [
                    "0"
                ]
            },
            "thumbnail_size": "thumbnail",
            "thumbnail_images": {
                "full": {
                    "url": "http://jantasamachar.com/wp-content/uploads/2017/08/nation-1.jpg",
                    "width": 1900,
                    "height": 600
                },
                "thumbnail": {
                    "url": "http://jantasamachar.com/wp-content/uploads/2017/08/nation-1-150x150.jpg",
                    "width": 150,
                    "height": 150
                },
                "medium": {
                    "url": "http://jantasamachar.com/wp-content/uploads/2017/08/nation-1-300x95.jpg",
                    "width": 300,
                    "height": 95
                },
                "medium_large": {
                    "url": "http://jantasamachar.com/wp-content/uploads/2017/08/nation-1-768x243.jpg",
                    "width": 768,
                    "height": 243
                },
                "large": {
                    "url": "http://jantasamachar.com/wp-content/uploads/2017/08/nation-1-1024x323.jpg",
                    "width": 1024,
                    "height": 323
                }
            }
        },

1 个答案:

答案 0 :(得分:1)

最好的方法是使用 GSON

  1. 在gradle.build
  2. 中添加GSON依赖项
  3. 为上面显示的响应创建GSON类。您可以使用此在线工具 - http://www.jsonschema2pojo.org
  4. 将改装响应类型设置为该类。
  5. 这样你可以使用改装

    获得上述响应类型