gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:49:52 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com