Retrofit抱怨这个json不是数组

时间:2018-11-14 20:16:23

标签: android json gson retrofit2 jsonparser

我知道这个问题已经被问过很多次了,但是我没有找到解决方法。

Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $

我的json,我希望有一个数组Model []:

[{
    "id": 1,
    "category": "Category 1",
    "question": "Bla bla",
    "answer": "<p>Bla bla bla.</p>\n",
    "lastModified": "2018-05-24T12:28:37.41"
},{
    "id": 2,
    "category": "Category 1",
    "question": "Bla bla",
    "answer": "<p>Bla bla bla.</p>\n",
    "lastModified": "2018-05-24T12:28:37.41"
},
{
    "id": 3,
    "category": "Category 1",
    "question": "Bla bla",
    "answer": "<p>Bla bla bla.</p>\n",
    "lastModified": "2018-05-24T12:28:37.41"
},
{
    "id": 4,
    "category": "Category 1",
    "question": "Bla bla",
    "answer": "<p>Bla bla bla.</p>\n",
    "lastModified": "2018-05-24T12:28:37.41"
}]

如果我从服务器复制响应,并且:

final String response = "...";
final Gson gson = new Gson();
final FAQ[] faqs = gson.fromJson(response, FAQ[].class);
Log.d("TAG", "TEST: " + faqs[0].getAnswer());

这正在按预期方式工作...服务器响应可能出了什么问题?

编辑:界面:

@Headers({
        "Accept-Encoding: gzip, deflate, br",
        "Accept-Language: it-IT"
})
@GET("faq")
Call<FAQ[]> getFAQs(@Query(Constants.API_PARAM_PAGE) int page,
                    @Query(Constants.API_PARAM_PAGE_SIZE) int pageSize);

1 个答案:

答案 0 :(得分:1)

解决了我自己的问题:

"Accept-Encoding: gzip, deflate, br"

如果您提供该标头,则Retrofit希望您将提供自己的解压缩实现。我需要放假。