IllegalStateException:预期BEGIN_OBJECT但是BEGIN_ARRAY

时间:2017-03-14 14:18:07

标签: java android json

我试图学习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);
}

ApplicationModule:

    @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();
}

最后是Mapper:

    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 $。对此有何解决方案?非常感谢你。

2 个答案:

答案 0 :(得分:0)

在我看来,问题出在mApiService.getPosts();方法。 请从mApiService添加代码。可能你有这样的事情:

Call<YourClass> getPosts();

将其更改为

Call<List<YourClass>> getPosts();

抱歉我的英文......

答案 1 :(得分:0)

因为我使用的是Json Server,所以我必须从

更改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"
        }
}

{
    "posts": {
        "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"
            }
}