以下是json响应的示例。您可以看到以下字段:news,number_likes,number_comment,number_post,user,owner始终存在。但是像place,place_mark,comment,album,photo这样的字段并不总是存在。这取决于新闻的类型。
{
"result": true,
"news_list": [
{
"news": {
"id": "67",
"text": "",
"text_post": "",
"date_create": "1460321713"
},
"number_likes": "0",
"number_comment": "0",
"number_post": "0",
"user": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/08382.jpeg",
"last_activity": "1460035798"
},
"owner": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/08382.jpeg",
"last_activity": "1460035798"
},
"place": {
"id": "1",
"name": "test",
"avatar": ""
},
"place_mark": {
"id": "1",
"date_create": "1460321699"
}
},
{
"news": {
"id": "66",
"text": "",
"text_post": "",
"date_create": "1460321623"
},
"number_likes": "0",
"number_comment": "0",
"number_post": "0",
"user": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/08382.jpeg",
"last_activity": "1460035798"
},
"owner": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/08082.jpeg",
"last_activity": "1460035798"
},
"place": {
"id": "1",
"name": "test",
"avatar": ""
},
"comment": {
"id": "1",
"parent_id": "",
"text": "qwer",
"rating": "0",
"date_create": "1460321472"
},
"photos": []
},
{
"news": {
"id": "65",
"text": "new news",
"text_post": "",
"date_create": "1460320767"
},
"number_likes": "0",
"number_comment": "0",
"number_post": "0",
"user": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/08082.jpeg",
"last_activity": "1460035798"
},
"owner": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/0882.jpeg",
"last_activity": "1460035798"
},
"photos": [
{
"photo": {
"id": "84",
"name": "",
"filename": "2d20f883cb023a1ff14f32d0ee79a02c.jpeg",
"mime_type": "image/jpeg",
"height": "540",
"width": "442",
"link": "http://base_url/userPhotos/41/album/original/29a02c.jpeg"
}
},
{
"photo": {
"id": "85",
"name": "",
"filename": "2d20f883cb023a1ff14f32d0ee79a02c.jpeg",
"mime_type": "image/jpeg",
"height": "540",
"width": "442",
"link": "http://base_url/userPhotos/41/album/original/2d20f2c.jpeg"
}
}
]
},
{
"news": {
"id": "64",
"text": "",
"text_post": "",
"date_create": "1460320759"
},
"number_likes": "0",
"number_comment": "0",
"number_post": "0",
"user": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/0809fb.jpeg",
"last_activity": "1460035798"
},
"owner": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/0882.jpeg",
"last_activity": "1460035798"
},
"album": {
"id": "34",
"name": "asdfgh",
"description": "",
"position": ""
},
"photo": ""
}
]
}
这是我对此回复的模型:
public class GetNewsResponse {
private String result;
private List<NewsList> news_list;
private Errors errors;
public String getResult() {
return result;
}
public List<NewsList> getNews_list() {
return news_list;
}
public Errors getErrors() {
return errors;
}
public class NewsList {
private String number_likes;
private String number_comment;
private String number_post;
private News news;
private User user;
private Owner owner;
private Place place;
private PlaceMark place_mark;
private Comment comment;
private Album album;
private List<Photo2> photos;
public String getNumber_likes() {
return number_likes;
}
public String getNumber_comment() {
return number_comment;
}
public String getNumber_post() {
return number_post;
}
public News getNews() {
return news;
}
public User getUser() {
return user;
}
public Owner getOwner() {
return owner;
}
public Place getPlace() {
return place;
}
public PlaceMark getPlaceMark() {
return place_mark;
}
public Comment getComment() {
return comment;
}
public Album getAlbum() {
return album;
}
public List<Photo2> getPhotos() {
return photos;
}
}
public class News {
private String id;
private String text;
private String text_post;
private String date_create;
public String getId() {
return id;
}
public String getText() {
return text;
}
public String getDate_create() {
return date_create;
}
public String getText_post() {
return text_post;
}
}
public class User {
private String id;
private String name;
private String last_name;
private String avatar;
private String last_activity;
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getLast_name() {
return last_name;
}
public String getAvatar() {
return avatar;
}
public String getLast_activity() {
return last_activity;
}
}
public class Owner {
private String id;
private String name;
private String last_name;
private String avatar;
private String last_activity;
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getLast_name() {
return last_name;
}
public String getAvatar() {
return avatar;
}
public String getLast_activity() {
return last_activity;
}
}
public class Place {
private String id;
private String name;
private String avatar;
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getAvatar() {
return avatar;
}
}
public class PlaceMark {
private String id;
private String date_create;
public String getId() {
return id;
}
public String getDate_create() {
return date_create;
}
}
public class Comment {
private String id;
private String parent_id;
private String text;
private String rating;
private String date_create;
public String getId() {
return id;
}
public String getParent_id() {
return parent_id;
}
public String getRating() {
return rating;
}
public String getText() {
return text;
}
public String getDate_create() {
return date_create;
}
}
public class Album {
private String id;
private String name;
private String description;
private String position;
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public String getPosition() {
return position;
}
}
public class Photo2 {
private Object photo;
public Object getPhoto() {
return photo;
}
}
public class Photo {
private String id;
private String name;
private String filename;
private String mime_type;
private String height;
private String width;
private String link;
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getFilename() {
return filename;
}
public String getMime_type() {
return mime_type;
}
public String getHeight() {
return height;
}
public String getWidth() {
return width;
}
public String getLink() {
return link;
}
}
public class Errors {
private String text;
public String getText() {
return text;
}
}
}
这是我的api方法实现:
public void getNewsMethod() {
sm.getNews(user_id, limit, offset, photo_size, avatar_size, all, new Callback(){
@Override
public void onResponse(Call<GetNewsResponse> call, Response<GetNewsResponse> response) {
List<GetNewsResponse.NewsList> list = response.body().getNews_list();
instantiateAdapter(list);
}
@Override
public void onFailure(Call<GetNewsResponse> call, Throwable t) {
Toast.makeText(MyNewsActivity.this, "get news failure", Toast.LENGTH_SHORT).show();
}
});
}
我使用翻新2.0.1和原生GSonConverterFactory:
private static Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
当照片json数组为空时,这是我在json反序列化时的错误:
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第9480行为STRING路径$ .news_list [11] .photos [0] .photo
请帮帮我!我需要做些什么来进行正确的json反序列化?也许有人有编写自定义转换器工厂的经验或根据我的问题给我任何建议? 感谢。
答案 0 :(得分:0)
抓住你的json回应的战利品。最后一个NewsList对象存在问题。
{
"news": {
"id": "64",
"text": "",
"text_post": "",
"date_create": "1460320759"
},
"number_likes": "0",
"number_comment": "0",
"number_post": "0",
"user": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/0809fb.jpeg",
"last_activity": "1460035798"
},
"owner": {
"id": "41",
"name": "Jimmy",
"last_name": "",
"avatar": "http://base_url/userPhotos/41/album/original/0882.jpeg",
"last_activity": "1460035798"
},
"album": {
"id": "34",
"name": "asdfgh",
"description": "",
"position": ""
},
"photo": "" // <- the problem
}
根据您的规格,您应该会收到照片列表。但是你收到了带有字符串的标签照片。请检查您的网络服务。