Home > front end >  Error while calling service with retrofit Expected BEGIN_ARRAY but was STRING at line 1 column 1 pat
Error while calling service with retrofit Expected BEGIN_ARRAY but was STRING at line 1 column 1 pat

Time:11-09

I'm struggling with this error since days. I've already tried to Clean Project and Rebuild it, but it didn't work for me.

Here is my retrofit call

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

My gson and retrofit declaration:

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

private List<FormCheckList> checks;

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

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

My response model from 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"
}]

But still having issues with it. I think the json response is also well formatted.

CodePudding user response:

Please what variable is used to save thischecks = response.body(); this "check" I think the "check" variable is a String, check to confirm else change it to return your model class.

CodePudding user response:

The problem was that the signature of API was wrong and also the returning List was wrongly configured from backend.

  • Related