从Web服务获取数组返回错误结果

时间:2018-07-22 12:53:33

标签: android

我的Web服务返回以下数组结构:

[
    {
        "id": 43525,
        "date": "2018-07-21T04:43:19",
        "date_gmt": "2018-07-21T01:13:19",
        "guid": {
            "rendered": "http://test.com/?p=43525"
        },
        "modified": "2018-07-21T04:43:19",
        "modified_gmt": "2018-07-21T01:13:19",
        "slug": "..........",
        "status": "publish",
        "type": "post",
        "link": ".........",
        "title": {
            "rendered": "........."
        },
        "content": {
            "rendered": "........",
            "protected": false
        },
        "excerpt": {
            "rendered": "",
            "protected": false
        },
        "author": 1,
        "featured_media": 43547,
        "comment_status": "open",
        "ping_status": "open",
        "sticky": false,
        "template": "",
        "format": "standard",
        "meta": [ ],
        "categories": [
            1546
        ],
        "tags": [
            4153,
            4152
        ],
        }
    }
]

并且我正在尝试使用具有此类定义的对象来获取此数组:

WordpressFeeds类:

public class WordpressFeeds {
    private List<WordpressFeedItems> feedItemsList;

    public List<WordpressFeedItems> getFeedItemsList() {
        return feedItemsList;
    }

    public void setFeedItemsList(List<WordpressFeedItems> feedItemsList) {
        this.feedItemsList = feedItemsList;
    }
}

WordpressFeedItems类:

class WordpressFeedItems {
        @SerializedName("id")
        @Expose
        private int id;

        @SerializedName("date")
        @Expose
        private String date;

        @SerializedName("modified")
        @Expose
        private String modified;

        @SerializedName("slug")
        @Expose
        private String slug;

        @SerializedName("status")
        @Expose
        private String status;

        @SerializedName("type")
        @Expose
        private String type;

        @SerializedName("link")
        @Expose
        private String link;

        @SerializedName("title")
        @Expose
        private String title;

        @SerializedName("content")
        @Expose
        private String content;

        @SerializedName("rendered")
        @Expose
        private String rendered;

        @SerializedName("excerpt")
        @Expose
        private String excerpt;

        @SerializedName("author")
        @Expose
        private String author;

        @SerializedName("featured_media")
        @Expose
        private String featured_media;

        @SerializedName("comment_status")
        @Expose
        private String comment_status;

        @SerializedName("sticky")
        @Expose
        private String sticky;

    //SETTER AND GETTERS
}

改装代码:

    Call<List<WordpressFeeds>> call = requestService.getWordpressFeeds();
    call.enqueue(new Callback<List<WordpressFeeds>>() {
        @Override
        public void onResponse(Call<List<WordpressFeeds>> call, final Response<List<WordpressFeeds>> response) {
            try {
                if (response.isSuccessful()) {
                    Log.e(response.body().toString());
                } else {
                }
            } catch (Exception e) {
            }
        }

        @Override
        public void onFailure(Call<List<WordpressFeeds>> call, Throwable t) {
            t.printStackTrace();
            if (t.getMessage() != null) Log.e("WordpressFeeds: ", t.getMessage());
        }
    });

改造界面:

public interface AoAoOnlineRestfulService {
    @Headers("Content-Type: application/json")
    @GET("/wp-json/wp/v2/posts/")
    Call<List<WordpressFeeds>> getWordpressFeeds();
}

我没有收到任何错误,但是我得到了错误的结果,因为下面的输出feedItemsList为NULL,应该是数组对象,而不是null:

response.body() = {ArrayList@5677}  size = 10
        0 = {WordpressFeeds@5679}
        feedItemsList = null
        shadow$_klass_ = {Class@5328} "class com.test.hello.Database.WordpressFeeds"
        shadow$_monitor_ = -1964381779

预先感谢

1 个答案:

答案 0 :(得分:0)

我认为您应该像下面那样更改服务界面:

public interface AoAoOnlineRestfulService {
   @Headers("Content-Type: application/json")
   @GET("/wp-json/wp/v2/posts/")
   Call<List<WordpressFeedItems>> getWordpressFeeds();
}

您的回复是List<WordpressFeedItems>而不是List<WordpressFeeds>