【发布时间】:2016-05-07 21:46:31
【问题描述】:
请我尝试了两种方法来解决这个问题,但都不起作用,我不知道发生了什么,但在它正常工作之前没有问题。 我有以下 json :
{
"_embedded" : {
"posts" : [ {
"postid" : 1,
"taggedusers" : null,
"hashtags" : null,
"createdAt" : "2016-05-07",
"commentsCount" : 0,
"likesCount" : 0,
"shareCount" : 0,
"description" : "nothing",
"post" : "nothing",
"_links" : {
"self" : {
"href" : "http://localhost:8080/posts/1"
},
"postsEntity" : {
"href" : "http://localhost:8080/posts/1"
},
"usersByUserid" : {
"href" : "http://localhost:8080/posts/1/usersByUserid"
},
"commentsesByPostid" : {
"href" : "http://localhost:8080/posts/1/commentsesByPostid"
}
}
}, {
"postid" : 2,
"taggedusers" : null,
"hashtags" : null,
"createdAt" : "2016-05-07",
"commentsCount" : 0,
"likesCount" : 0,
"shareCount" : 0,
"description" : "nothing",
"post" : "nothing",
"_links" : {
"self" : {
"href" : "http://localhost:8080/posts/2"
},
"postsEntity" : {
"href" : "http://localhost:8080/posts/2"
},
"usersByUserid" : {
"href" : "http://localhost:8080/posts/2/usersByUserid"
},
"commentsesByPostid" : {
"href" : "http://localhost:8080/posts/2/commentsesByPostid"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/users/6/postsesByUserid"
}
}
}
我尝试在改造中使用以下内容阅读它:
Retrofit retrofit = RetrofitGenerator.initRetrofit(CacheUtils.readSharedPreference(getActivity()).getToken());
PostService postService= retrofit.create(PostService.class);
postService.getUsersPost((int) CacheUtils.readSharedPreference(getActivity()).getUserID()).enqueue(new Callback<List<Post>>() {
@Override
public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
if(response.isSuccessful()){
List<Post> postEntitiyList = response.body();
}
}
@Override
public void onFailure(Call<List<Post>> call, Throwable t) {
Log.e("teeest",t.getMessage());
}
});
对于接口:
public interface PostService {
@Headers("Content-Type:application/json")
@GET("/posts")
Call<List<Post>> getAllPosts();
@Headers("Content-Type:application/json")
@GET("users/{id}/postsesByUserid")
Call<List<Post>> getUsersPost(@Path("id") int id);
}
但我遇到了这个问题:预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 $ 处是 BEGIN_OBJECT 我试图在另一个类中制作 Post Pojo,如下所示:
public class PostEntitiy {
private Post post;
public PostEntitiy(Post post) {
this.post = post;
}
public Post getPost() {
return post;
}
public void setPost(Post post) {
this.post = post;
}
}
然后我用List还是遇到同样的问题,我用什么错了?
【问题讨论】:
标签: android gson retrofit retrofit2