获取嵌套的对象数组,改造android

时间:2018-09-10 12:39:16

标签: java android arrays json retrofit

我正在使用Retrofit android库和Gson转换器从服务器获取JSON响应, 这是回应。收到响应后,我的应用程序在onCreate()API中崩溃。谁能建议我下面的代码有什么问题?

{
"content": [
    {
        "id": 1,
        "title": "Xrp the standard",
        "desc": "Hodl till you die",
        "createdBy": {
            "id": 1,
            "username": "1",
            "name": "Radovan"
        },
        "creationDateTime": {
            "epochSecond": 1533679200,
            "nano": 0
        },
        "photo": "to_the_moon.jpg",
        "tags": "tagovi",
        "likes": 12,
        "dislikes": 2,
        "comments": [
            {
                "id": 1,
                "title": "Bravo nasi",
                "desc": "Samo sloga Srbina spasava",
                "createdBy": {
                    "id": 2,
                    "username": "",
                    "name": ""
                },
                "creationDateTime": {
                    "epochSecond": 1533679200,
                    "nano": 0
                },
                "likes": 3,
                "dislikes": 4
            }
        ]
    }
],
"page": 0,
"size": 30,
"totalElements": 1,
"totalPages": 1,
"last": true
}

这是我的Retro客户,

public class RetroClient {

private static final String BASE_URL = "http://192.189.0.27:8080/";

private static Retrofit getRetrofitInstance() {
    return new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    }

public static RestAPI getRestAPI() {
    return getRetrofitInstance().create(RestAPI.class);
}



}

这是我的其余api调用

@GET("api/posts")
    Call<Example> getPosts();

我的示例课

public class Example {

@SerializedName("content")
@Expose
private List<Content> content;
@SerializedName("page")
@Expose
private Integer page;
@SerializedName("size")
@Expose
private Integer size;
@SerializedName("totalElements")
@Expose
private Integer totalElements;
@SerializedName("totalPages")
@Expose
private Integer totalPages;
@SerializedName("last")
@Expose
private Boolean last;

public Example() {
}

public Example(List<Content> content, Integer page, Integer size, Integer totalElements, Integer totalPages, Boolean last) {
    super();
    this.content = content;
    this.page = page;
    this.size = size;
    this.totalElements = totalElements;
    this.totalPages = totalPages;
    this.last = last;
}

//Getters and setters

我的Content类(需要访问的嵌套对象数组,作为响应,“ content”)

public class Content {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("desc")
@Expose
private String desc;
@SerializedName("createdBy")
@Expose
private CreatedBy createdBy;
@SerializedName("creationDateTime")
@Expose
private CreationDateTime creationDateTime;
@SerializedName("photo")
@Expose
private String photo;
@SerializedName("tags")
@Expose
private String tags;
@SerializedName("likes")
@Expose
private Integer likes;
@SerializedName("dislikes")
@Expose
private Integer dislikes;
@SerializedName("comments")
@Expose
private List<CommentResponse> comments;

public Content() {
}

public Content(Integer id, String title, String desc, CreatedBy createdBy, CreationDateTime creationDateTime, String photo, String tags, Integer likes, Integer dislikes, List<CommentResponse> comments) {
    super();
    this.id = id;
    this.title = title;
    this.desc = desc;
    this.createdBy = createdBy;
    this.creationDateTime = creationDateTime;
    this.photo = photo;
    this.tags = tags;
    this.likes = likes;
    this.dislikes = dislikes;
    this.comments = comments;
}
//Getters and setters

最后,我在onCreate中的回调方法

RestAPI rest_api = RetroClient.getRestAPI();
    Call<Example> call = rest_api.getPosts();


    call.enqueue(new Callback<Example>() {
        @Override
        public void onResponse(Call<Example>  call, Response<Example> response) {

            if( response.isSuccessful()) {

              //here, I need to get "content" so I could fill up list in code below,
 //tried almost everything, but activity crashes when created
 postsList = response.body().getContent();


                for(int i = 0; i < postsList.size(); i++) {
                    Content content = postsList.get(i);
                    pAdapter.addPost(content);
                }

            }
            else {
                int sc = response.code();
                switch (sc) {

                }
            }

        }

        @Override
        public void onFailure(Call<Example> call, Throwable t) {
            Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

2 个答案:

答案 0 :(得分:1)

在Example类中为列表“内容”添加获取器和设置器,然后执行“ response.body()。getContent()”以获取列表。

postsList = response.body().getContent();

答案 1 :(得分:0)

您的Pojos应该实现Parcelable接口

   public class Example implements Parcelable {
//your members
}