gpt4 book ai didi

java - 非法状态异常 : Expected BEGIN_OBJECT but was BEGIN_ARRAY

转载 作者:太空宇宙 更新时间:2023-11-04 11:40:58 29 4
gpt4 key购买 nike

我正在努力学习 MVP。我在教程和 YouTube 视频的帮助下编写了代码。首先,我使用了 Json2Class 插件。之后,我创建了一个 Json-Server,只是为了从 REST API 获取一个 json 文件来进行测试。

{
"posts": [{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
{
"userId": 1,
"id": 2,
"title": "qui est esse",
"body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
},
{
"userId": 1,
"id": 3,
"title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
"body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut"
},
{
"userId": 1,
"id": 4,
"title": "eum et est occaecati",
"body": "ullam et saepe reiciendis voluptatem adipisci\nsit amet autem assumenda provident rerum culpa\nquis hic commodi nesciunt rem tenetur doloremque ipsam iure\nquis sunt voluptatem rerum illo velit"
}]}

这是 json 文件的一瞥。我通过插件转换为Java类。然后在 Presenter 中我有这个方法

    public void getPosts() {
getView().onShowDialog("Loading posts.....");
Observable<PostsResponse> postsResponseObservable = mApiService.getPosts();
subscribe(postsResponseObservable, this);
}

应用程序模块:

    @Singleton
@Provides
Retrofit provideRetrofit(@Named("ok-1") OkHttpClient client, GsonConverterFactory converterFactory,
RxJava2CallAdapterFactory adapterFactory){
return new Retrofit.Builder()
.baseUrl(mBaseUrl)
.addConverterFactory(converterFactory)
.addCallAdapterFactory(adapterFactory)
.client(client)
.build();
}

一切都已提供,但这里是 GsonConverterFactory:

    @Singleton 
@Provides
GsonConverterFactory provideGsonConverterFactory(){
return GsonConverterFactory.create();
}

最后是映射器:

    public List<Post> mapPosts(PostsResponse response){
List<Post> postList = new ArrayList<>();
if (response != null) {
PostsResponsePosts[] responsePosts = response.getPosts();
if (responsePosts != null) {
for (PostsResponsePosts post : responsePosts) {
Post mPost = new Post();
mPost.setUserId(post.getUserId());
mPost.setId(post.getId());
mPost.setTitle(post.getTitle());
mPost.setBody(post.getBody());
postList.add(mPost);
}
}
}
return postList;
}

但是我得到IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $。有什么办法解决这个问题吗?非常感谢。

最佳答案

我认为问题出在 mApiService.getPosts();方法。请添加来自 mApiService 的代码。也许你有这样的事情:

Call<YourClass> getPosts();

更改为

Call<List<YourClass>> getPosts();

抱歉我的英语...

关于java - 非法状态异常 : Expected BEGIN_OBJECT but was BEGIN_ARRAY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42788468/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com