使用GSON进行改造:response.body()返回带有空字段的对象

时间:2016-05-08 00:46:27

标签: android gson retrofit retrofit2

我正在尝试使用GSON Library使用Retrofit读取以下JSON,我遇到的问题是返回的帖子列表为null我不知道如何修复此错误我检查了类的命名,一切都正确

JSON我想读:

{
  "_embedded" : {
    "posts" : [ {
      "postid" : 1,
      "taggedusers" : null,
      "hashtags" : null,
      "createdAt" : "2016-05-07",
      "commentsCount" : 0,
      "likesCount" : 0,
      "shareCount" : 0,
      "description" : "nothing",
      "post" : "nothing",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/posts/1"
        },
        "postsEntity" : {
          "href" : "http://localhost:8080/posts/1"
        },
        "usersByUserid" : {
          "href" : "http://localhost:8080/posts/1/usersByUserid"
        },
        "commentsesByPostid" : {
          "href" : "http://localhost:8080/posts/1/commentsesByPostid"
        }
      }
    }, {
      "postid" : 2,
      "taggedusers" : null,
      "hashtags" : null,
      "createdAt" : "2016-05-07",
      "commentsCount" : 0,
      "likesCount" : 0,
      "shareCount" : 0,
      "description" : "nothing",
      "post" : "nothing",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/posts/2"
        },
        "postsEntity" : {
          "href" : "http://localhost:8080/posts/2"
        },
        "usersByUserid" : {
          "href" : "http://localhost:8080/posts/2/usersByUserid"
        },
        "commentsesByPostid" : {
          "href" : "http://localhost:8080/posts/2/commentsesByPostid"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/users/6/postsesByUserid"
    }
  }
}

这是我使用的界面:

public interface PostService {
    @Headers("Content-Type:application/json")
    @GET("/posts")
    Call<List<Post>> getAllPosts();

    @Headers("Content-Type:application/json")
    @GET("users/{id}/postsesByUserid")
    Call<Embedded> getUsersPost(@Path("id") int id);
}

我得到的答案:回复{protocol = http / 1.1,code = 200,message = OK,url = http://192.168.1.5:8080/users/6/postsesByUserid}

对于我使用的类以下内容:

public class Embedded {

public List<Post> posts = new ArrayList<>();

    public List<Post> getPosts() {
        return posts;
    }

    public void setPosts(List<Post> posts) {
        this.posts = posts;
    }
}

public class Top {
    @SerializedName("_embedded")
    public Embedded embedded;

}


public class Post {
    private Integer postid;
    private String taggedusers;
    private String hashtags;
    private String createdAt;
    private Integer commentsCount;
    private Integer likesCount;
    private Integer shareCount;
    private String description;
    private String post;
    private String commentsesByPostid;
    private String usersByUserid;



  public Post(Integer postid, String taggedusers, String hashtags, String createdAt, Integer commentsCount, Integer likesCount, Integer shareCount, String description, String post, String commentsesByPostid, String usersByUserid) {
            this.postid = postid;
            this.taggedusers = taggedusers;
            this.hashtags = hashtags;
            this.createdAt = createdAt;
            this.commentsCount = commentsCount;
            this.likesCount = likesCount;
            this.shareCount = shareCount;
            this.description = description;
            this.post = post;
            this.commentsesByPostid = commentsesByPostid;
            this.usersByUserid = usersByUserid;
        }

        public Integer getPostid() {
            return postid;
        }

        public void setPostid(Integer postid) {
            this.postid = postid;
        }

        public String getTaggedusers() {
            return taggedusers;
        }

        public void setTaggedusers(String taggedusers) {
            this.taggedusers = taggedusers;
        }

        public String getHashtags() {
            return hashtags;
        }

        public void setHashtags(String hashtags) {
            this.hashtags = hashtags;
        }

        public String getCreatedAt() {
            return createdAt;
        }

        public void setCreatedAt(String createdAt) {
            this.createdAt = createdAt;
        }

        public Integer getCommentsCount() {
            return commentsCount;
        }

        public void setCommentsCount(Integer commentsCount) {
            this.commentsCount = commentsCount;
        }

        public Integer getLikesCount() {
            return likesCount;
        }

        public void setLikesCount(Integer likesCount) {
            this.likesCount = likesCount;
        }

        public Integer getShareCount() {
            return shareCount;
        }

        public void setShareCount(Integer shareCount) {
            this.shareCount = shareCount;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getPost() {
            return post;
        }

        public void setPost(String post) {
            this.post = post;
        }

        public String getCommentsesByPostid() {
            return commentsesByPostid;
        }

        public void setCommentsesByPostid(String commentsesByPostid) {
            this.commentsesByPostid = commentsesByPostid;
        }

        public String getUsersByUserid() {
            return usersByUserid;
        }

        public void setUsersByUserid(String usersByUserid) {
            this.usersByUserid = usersByUserid;
        }
    }

1 个答案:

答案 0 :(得分:3)

您需要替换PostService getUsersPost(@Path("id") int id);方法的返回类型。

getUsersPost应该返回Call<Top>而不是Call<Embedded>,如下所示:

public interface PostService {
    @Headers("Content-Type:application/json")
    @GET("users/{id}/postsesByUserid")
    Call<Top> getUsersPost(@Path("id") int id);
}