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

标签 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
}

这是我的复古客户端,

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

我的内容类(我需要访问的对象的嵌套数组,响应中的“内容”)

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();
        }
    });

最佳答案

在示例类中为列表“内容”添加 getter 和 setter,并执行“response.body().getContent()”来获取列表。

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

关于java - 获取对象的嵌套数组,改造android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52258018/

相关文章:

python - 从 .csv 文件中读取值并将它们转换为 float 组

java - 使用封装时是否有一些共享方法行为的技巧?

android - 应用程序启动时以编程方式调用 rawQuery

android - Gradle Wrapper在多个项目中一次又一次下载gradle行为

java - 带自定义适配器的 ListView 不加载标题数组,仅使用抽屉导航加载 0

用于查找数组最大公共(public)子集的 Objective-C 算法?

java - 在java中用字符串替换正则表达式

java - 如何使用Java在单个终端中执行多个命令?

java - 从泛型类派生类型

java - 如何从数组中选择最大值?