Lio*_*789 23 java android json gson retrofit
我正在使用我的json对象返回一个结果数组,我正在尝试使用我的customObjectResponse类来拉出每个对象中的每个字段...它期望一个对象的问题所以我如何编辑我的类允许它接受一个对象数组,然后可以调用每个对象的字段...我很困惑,需要添加什么:
以下是要传递的内容的响应示例:
[
{
itemId: 'dfsdfsdf343434',
name: 'tests',
picture: '6976-7jv8h5.jpg',
description: 'testy.',
dateUpdated: 1395101819,
}
]
Run Code Online (Sandbox Code Playgroud)
这是我的响应对象类:
public class ObjResponse{
private String itemId;
private String name;
private String picture;
private String description;
private String location;
private int dateUpdated;
private String msg;
//gridview constructor
public ObjResponse(String picture) {
this.picture = picture;
}
//public constructor
public ObjResponse() {
}
public String getItemId() {
return itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getDateUpdated() {
return dateUpdated;
}
public void setDateUpdated(int dateUpdated) {
this.dateUpdated = dateUpdated;
}
public String getMsg() {
return msg;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试,但不工作,即使我将类分成他们自己的文件:
Data passed in:
items: [{obj1: "A", obj2: ["c", "d"]}, {etc...}]
public class Response {
public class List<Custom> {
private List<Custom> items;
}
public class Custom {
private String obj1;
private List<Obj2> obj2;
}
public Class Obj2 {
private String letters;
}
}
Run Code Online (Sandbox Code Playgroud)
Lio*_*789 34
我最后只是在回调中调用了一个customObject列表,它完成了这项工作......
new Callback<List<ObjResponse>>() {
Run Code Online (Sandbox Code Playgroud)
col*_*ain 11
我原本无法了解如何OP解决他的问题,但经过几天的调试后我终于想出了如何解决这个问题.
所以你基本上有这样的格式的数据(JSON数组的JSON对象):
[
{
...
}
]
Run Code Online (Sandbox Code Playgroud)
您为数据建模并包含getter和setter方法的类只不过是典型的POJO.
public class Person implements Serializable {
@SerializedName("Exact format of your json field name goes here")
private String firstName;
// Getters and Setters....
}
Run Code Online (Sandbox Code Playgroud)
在包含RESTful注释的界面中,您希望将呼叫转换为:
之前:
public interface APInterface {
@GET("SOME URL TO YOUR JSON ARRAY")
Call<Person>(...)
}
Run Code Online (Sandbox Code Playgroud)
后:
public interface APInterface {
@GET("SOME URL TO YOUR JSON ARRAY")
Call<List<Person>>(...)
}
Run Code Online (Sandbox Code Playgroud)
在你的Android活动要所有呼叫转换的形式Call<Person>来Call<List<Person>>
最后,当进行初始异步请求调用时,您将希望像这样转换回调.
call.enqueue(new Callback<List<Person>>() {
@Override
public void onResponse(Call<List<Person>> call, Response<List<Person>> response) {
if(response.isSuccessful()){
List<Person> person = response.body();
// Can iterate through list and grab Getters from POJO
for(Person p: person){...}
} else {
// Error response...
}
}
@Override
public void onFailure(Call<List<Person>> call, Throwable t) {...}
});
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助那些因上述接受的答案而迷失的人.
| 归档时间: |
|
| 查看次数: |
22172 次 |
| 最近记录: |