使用改造调用服务时出错 Expected BEGIN_ARRAY 但在第 1 行第 1 列路径 $ 处为 STRING

Posted

技术标签:

【中文标题】使用改造调用服务时出错 Expected BEGIN_ARRAY 但在第 1 行第 1 列路径 $ 处为 STRING【英文标题】:Error while calling service with retrofit Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ 【发布时间】:2021-12-22 01:25:39 【问题描述】:

我几天以来一直在努力解决这个错误。我已经尝试清理项目并重建它,但它对我不起作用。

这是我的改造电话

@Headers("Content-Tyoe: application/json")
@GET("api/formchecks/available/job/id")
Call<List<FormCheckList>> getAvailableFormCheckListsByJob(@Path("id") int id, @Header("Authorization") String auth);

我的 gson 和改造声明:

Gson gson = new GsonBuilder()
        .setLenient()
        .create();

private List<FormCheckList> checks;

retrofit = new Retrofit.Builder()
            .baseUrl(RetrofitInterface.API_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

我的 call.enqueue

 RetrofitInterface service = retrofit.create(RetrofitInterface.class);        
 Call<List<FormCheckList>> call = service.getAvailableFormCheckListsByJob(id, auth);
 call.enqueue(new Callback<List<FormCheckList>>() 
        @Override
        public void onResponse(Call<List<FormCheckList>> call, Response<List<FormCheckList>> response) 
            
            ErrorResponse res;
            Log.d("HERE", "CHECKS: " + (response.body() != null));
            switch (response.code()) 
                case 404:

                    try 
                        res = gson.fromJson(response.errorBody().string(), ErrorResponse.class);
                        customAlert.setTxtAlert(res.message);
                        customAlert.setType("info");
                        customAlert.startLoadingDialog();
                     catch (IOException e) 
                        Log.d("HERE2", "checks " + response.body());
                        e.printStackTrace();
                        customAlert.setTxtAlert(e.getMessage());
                        customAlert.setType("error");
                        customAlert.startLoadingDialog();
                    

                    break;
                case 400:
                    try 
                        res = gson.fromJson(response.errorBody().string(), ErrorResponse.class);
                        customAlert.setTxtAlert(res.message);
                        customAlert.setType("warning");
                        customAlert.startLoadingDialog();
                     catch (IOException e) 
                        Log.d("HERE1", "checks " + response.body());
                        e.printStackTrace();
                        customAlert.setTxtAlert(e.getMessage());
                        customAlert.setType("error");
                        customAlert.startLoadingDialog();
                    
                    break;
                case 200:                        
                    checks = response.body();                        
                    break;
                default:
                    break;
            

        

        @Override
        public void onFailure(Call<List<FormCheckList>> call, Throwable t)                 
            customAlert.setTxtAlert(t.getMessage());
            customAlert.setType("error");
            customAlert.startLoadingDialog();
        
    );

我的 Postman 响应模型

[

    "id": 119,
    "formParamSetupId": 8893,
    "formParamSetup": 
        "id": 8893,
        "referIndex": "1.1",
        "formParam": 
            "id": 3,
            "description": "Larghezza",
            "lastUpdate": "2020-08-24T08:52:05.0300464"
        ,
        "paramExtraValue": "SPC",
        "dimensionalSurvey": 
            "value": 24.8,
            "posTolerance": 0.5,
            "negTolerance": 0.25,
            "surveyType": null
        
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.0303808"
,

    "id": 120,
    "formParamSetupId": 8894,
    "formParamSetup": 
        "id": 8894,
        "referIndex": "1.2",
        "formParam": 
            "id": 5,
            "description": "Verifica cavità contatto",
            "lastUpdate": "2020-08-24T08:52:05.0696232"
        ,
        "paramExtraValue": null,
        "dimensionalSurvey": null
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.0330752"
,

    "id": 121,
    "formParamSetupId": 8895,
    "formParamSetup": 
        "id": 8895,
        "referIndex": "1.3",
        "formParam": 
            "id": 90,
            "description": "Verifica assenza di bava imbocco cavità contatto",
            "lastUpdate": "2020-08-31T12:04:00"
        ,
        "paramExtraValue": null,
        "dimensionalSurvey": null
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.0355646"
,

    "id": 122,
    "formParamSetupId": 8896,
    "formParamSetup": 
        "id": 8896,
        "referIndex": "2.1",
        "formParam": 
            "id": 38,
            "description": "Verifica assenza di : bruciature,crepe,bave,incompletezze,deformazioni",
            "lastUpdate": "2020-08-24T08:52:05.7714885"
        ,
        "paramExtraValue": null,
        "dimensionalSurvey": null
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.0381673"
,

    "id": 123,
    "formParamSetupId": 8897,
    "formParamSetup": 
        "id": 8897,
        "referIndex": "2.2",
        "formParam": 
            "id": 51,
            "description": "Verifica inserimento controparte",
            "lastUpdate": "2020-08-24T08:52:06.0401384"
        ,
        "paramExtraValue": "maschio (vedere ciclo)",
        "dimensionalSurvey": null
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.0405131"
,

    "id": 124,
    "formParamSetupId": 8898,
    "formParamSetup": 
        "id": 8898,
        "referIndex": "2.3",
        "formParam": 
            "id": 54,
            "description": "Verifica inserimento wedge",
            "lastUpdate": "2020-08-24T08:52:06.1074036"
        ,
        "paramExtraValue": "female 54200225",
        "dimensionalSurvey": null
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.0429302"
,

    "id": 125,
    "formParamSetupId": 8899,
    "formParamSetup": 
        "id": 8899,
        "referIndex": "2.4",
        "formParam": 
            "id": 55,
            "description": "Verifica inserimento CPA",
            "lastUpdate": "2020-08-24T08:52:06.1264616"
        ,
        "paramExtraValue": "54200003",
        "dimensionalSurvey": null
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.0453145"
,

    "id": 126,
    "formParamSetupId": 8900,
    "formParamSetup": 
        "id": 8900,
        "referIndex": "2.5",
        "formParam": 
            "id": 56,
            "description": "Verifica inserimento retainer",
            "lastUpdate": "2020-08-24T08:52:06.1456205"
        ,
        "paramExtraValue": "54200234",
        "dimensionalSurvey": null
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.0476976"
,

    "id": 127,
    "formParamSetupId": 8901,
    "formParamSetup": 
        "id": 8901,
        "referIndex": "2.6",
        "formParam": 
            "id": 62,
            "description": "Verifica polarizzazione",
            "lastUpdate": "2020-08-24T08:52:06.2752349"
        ,
        "paramExtraValue": null,
        "dimensionalSurvey": null
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.050052"
,

    "id": 128,
    "formParamSetupId": 8903,
    "formParamSetup": 
        "id": 8903,
        "referIndex": "2.8",
        "formParam": 
            "id": 49,
            "description": "Verifica massima bava ammessa",
            "lastUpdate": "2020-08-24T08:52:05.9991429"
        ,
        "paramExtraValue": "nel gancio max 0.3 mm",
        "dimensionalSurvey": null
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.05241"
,

    "id": 129,
    "formParamSetupId": 8902,
    "formParamSetup": 
        "id": 8902,
        "referIndex": "2.7",
        "formParam": 
            "id": 39,
            "description": "Verifica presenza",
            "lastUpdate": "2020-08-24T08:52:05.7915265"
        ,
        "paramExtraValue": "numero cavità stampo, numero cavità contatti e numero di vendita 52772",
        "dimensionalSurvey": null
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.0548928"
,

    "id": 130,
    "formParamSetupId": 8904,
    "formParamSetup": 
        "id": 8904,
        "referIndex": "2.9",
        "formParam": 
            "id": 78,
            "description": "Etichette identificazione",
            "lastUpdate": "2020-08-24T08:52:06.6031977"
        ,
        "paramExtraValue": null,
        "dimensionalSurvey": null
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.0574036"
,

    "id": 131,
    "formParamSetupId": 8905,
    "formParamSetup": 
        "id": 8905,
        "referIndex": "2.10",
        "formParam": 
            "id": 79,
            "description": "Corretta quantità nelle scatole",
            "lastUpdate": "2020-08-24T08:52:06.6229642"
        ,
        "paramExtraValue": null,
        "dimensionalSurvey": null
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.0597732"
,

    "id": 132,
    "formParamSetupId": 5684,
    "formParamSetup": 
        "id": 5684,
        "referIndex": "4.1",
        "formParam": 
            "id": 86,
            "description": "Verifica presenza ultimo reclamo esposto",
            "lastUpdate": "2020-08-31T11:27:00"
        ,
        "paramExtraValue": null,
        "dimensionalSurvey": null
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.0636418"
,

    "id": 133,
    "formParamSetupId": 5685,
    "formParamSetup": 
        "id": 5685,
        "referIndex": "4.2",
        "formParam": 
            "id": 87,
            "description": "Verifica corretto funzionamento della bilancia",
            "lastUpdate": "2020-08-31T11:27:00"
        ,
        "paramExtraValue": null,
        "dimensionalSurvey": null
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.0662109"
,

    "id": 134,
    "formParamSetupId": 5686,
    "formParamSetup": 
        "id": 5686,
        "referIndex": "4.3",
        "formParam": 
            "id": 88,
            "description": "Verifica Termoregolatore",
            "lastUpdate": "2020-08-31T11:27:00"
        ,
        "paramExtraValue": null,
        "dimensionalSurvey": null
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.0684877"
,

    "id": 135,
    "formParamSetupId": 5687,
    "formParamSetup": 
        "id": 5687,
        "referIndex": "4.4",
        "formParam": 
            "id": 89,
            "description": "Verifica Metal Detector",
            "lastUpdate": "2020-08-31T11:27:00"
        ,
        "paramExtraValue": null,
        "dimensionalSurvey": null
    ,
    "formCheckListTimer": null,
    "formCheckListQualityCheck": null,
    "insertDate": "2021-11-08T12:23:18.0707119"
]

但还是有问题。 我认为json响应的格式也很好。

【问题讨论】:

【参考方案1】:

请问用什么变量来保存这个checks = response.body(); 这个“检查” 我认为“检查”变量是一个字符串,检查以确认否则更改它以返回您的模型类。

【讨论】:

您可以尝试的另一件事是检查您的模型类是否结构良好 checks 被声明为private List&lt;FormCheckList&gt; checks; 尝试进行检查,然后将 Object 转换为 response.body() 它一直在 onFailure 分支中进行【参考方案2】:

问题是API的签名错误,并且返回的List从后端配置错误。

【讨论】:

以上是关于使用改造调用服务时出错 Expected BEGIN_ARRAY 但在第 1 行第 1 列路径 $ 处为 STRING的主要内容,如果未能解决你的问题,请参考以下文章

使用改造从 api 获取数据时出错

Object_C 定义全局宏的颜色时,报“Expected identifier”的错误

使用序列更新表时出错

[Python]json对象转换出错expected string or buffer python

使用restkit api目标c调用Web服务时出错

解析参数“--change-batch”时出错:预期:“=”,接收到:“”用于输入: