我正在尝试创建Photo对象的Photos ArrayList。我想将Photo类与Photos类分开,因为它变得不可读。问题是我不再收到任何数据
值得注意的是,当我将Photo类嵌套在Photos类中时,这个WAS工作如下:
public class Photos {
@Expose private int page;
@Expose private int pages;
@Expose private int perpage;
@Expose private String total;
@Expose private ArrayList<Photo> photo = new ArrayList<Photo>();
//helpers
public ArrayList<Photo> getPhotos(){return photo;}
public void setPhotos(ArrayList<Photo> photos){this.photo = photos;}
//want to put this in it's own class
public class Photo {
//helpers
@Override
public String toString(){
return title;
}
public String getUrl(){return url_s;}
//GSON fields
@Expose private String id;
@Expose private String owner;
@Expose private String secret;
@Expose private String server;
@Expose private int farm;
@Expose private String title;
@Expose private int ispublic;
@Expose private int isfriend;
@Expose private int isfamily;
@Expose private String url_s;
}
}
改造:
new Callback<PhotosResponse>() {
@Override
public void success(PhotosResponse photosResponse, Response response) {
bus.post(new ImagesReceivedEvent(photosResponse.getPhotosObject().getPhotos()));
}
参数与JSON完全匹配,我通过以下响应收到了一个arraylist:
public class ImagesReceivedEvent {
private ArrayList<Photo> result;
public ImagesReceivedEvent(ArrayList<Photo> result){
Log.i(TAG, "arraylist for presenter");
this.result = result;
}
public ArrayList<Photo> getResult(){return result;}
}
这是收到回复的地方,但不再是:
public class PhotosResponse {
@Expose private Photos photos;
@Expose private String stat;
public Photos getPhotosObject(){return photos;}
public void setPhotosObject(Photos photos) {
this.photos = photos;
}
public String getStat() {
return stat;
}
public void setStat(String stat) {
this.stat = stat;
}
}
如何将这些类分开并仍将arrayList作为响应?我想将它们分开,因为我计划在每个类上使用Parcelable,在使用内部类时我需要跳过一些障碍。任何指导将不胜感激!感谢。
编辑:这是JSON有效负载:
{ "photos": { "page": 1, "pages": 10, "perpage": 100, "total": "1000",
"photo": [
{ "id": "18473086614", "owner": "130897025@N07", "secret": "b3c684c356", "server": "259", "farm": 1, "title": "My travels circa 2013. Summer days on #charlesbridge #charlesbridgeprague #prauge. upbound@upbound.net || 718-754-5850 #photographer #photography #canonphotography #canon #canondslr #canon600d #dslr #600d #travelphotography #europe #upboundonline #canonre", "ispublic": 1, "isfriend": 0, "isfamily": 0 },
{ "id": "18473090744", "owner": "131790787@N07", "secret": "2734055852", "server": "3705", "farm": 4, "title": "Untitled", "ispublic": 1, "isfriend": 0, "isfamily": 0 },
{ "id": "18473091934", "owner": "61308696@N00", "secret": "b40dbfcf15", "server": "401", "farm": 1, "title": "Climbing Acatenango", "ispublic": 1, "isfriend": 0, "isfamily": 0 },
{ "id": "18473092714", "owner": "39055811@N08", "secret": "e51f5a183b", "server": "3936", "farm": 4, "title": "DSCF1735.jpg", "ispublic": 1, "isfriend": 0, "isfamily": 0 }
] }, "stat": "ok" }
答案 0 :(得分:1)
事实证明我在命名约定上犯了一个错误:
@Expose private ArrayList<Photo> mPhotos = new ArrayList<Photo>();
我已将照片重命名为arrayList上的mPhotos,但忘记了GSON需要JSON字段的精确名称(原来是照片)。我把它改回了&#34;照片&#34;现在,arrayList填充在我的Photos对象上。
我需要向自己强调所有字段都必须符合JSON命名约定。
感谢。
答案 1 :(得分:0)
当使用Retrofit和GSON时,无法将您的响应“分离”到不同的类中,您必须自己以后再进行。使用所有信息接收您的大对象,然后将其打开到所需的类。
在我做过的项目中,我有一个包含Wrapper类的特定包来获取Retrofit的响应,然后我会手动将它映射到我实际需要的对象。希望它有所帮助。