0
請我試過兩種方法來解決這個問題,但都不起作用,我不知道發生了什麼,但在它沒有問題之前工作。 我有以下的JSON:Retrofit Gson:預計BEGIN_ARRAY,但是BEGIN_OBJECT(不重複)
{
"_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,但是BEGIN_OBJECT在第1行第2列路徑$ 我試圖在另一個類中創建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還是得到同樣的問題,我用什麼錯誤的東西?
那麼我怎麼讀?請?我應該使用_embed類 –
是的,您需要設計具有匹配JSON名稱和類型的屬性的Java對象層次結構。 – totoro
@ H-Ideas我編輯與入門的想法。我不擅長改造,所以這是我能做的最好的:-) – totoro