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

Viewed 368

I have an API, that for getting data I must send a Parameter of JSON. Here is the JSON that must need to be sent in the @Body to get the data.

{
    "ViewName": "Members_HousholdAdmin",
    "DataRequest":{
      "filter":[{
        "field":"",
        "logic":"",
        "operator":"",
        "value":""
       }],
        "offset":0,"take":0,
        "Sort":[{"field":"","dir":""}]
    },
    "parameters":[
    {"key": "%FundId","value": "1" }
    ]
}

It works in postman, but in Android, I have the following error:

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

I guess this is a problem in my models, but I can not fix it.

My Models :

public class SendParametersGetData {

    @SerializedName("ViewName")
    public String ViewName;

    @SerializedName("DataRequest")
    public DataRequest DataRequest = null;


    @SerializedName("Parameters")
    public List<Parameters> parameters = null;
}

public class DataRequest  {
    @SerializedName("take")
    public int take=0;

    @SerializedName("offset")
    public int offset=0;

    @SerializedName("filter")
    public ArrayList<Filter> filter;

    @SerializedName("Sort")
    public ArrayList<Sort> Sort;
}

public class Parameters {

    @SerializedName("value")
    public String value;

    @SerializedName("key")
    public String key;

    public Parameters(String key, String value) {
        this.value = value;
        this.key = key;
    }
}

There is 2 more models for sort and filter. I use retrofit & RXJava.

@POST("test.php")
Single<ArrayList<Members>> getMembers(@Body SendParametersGetData sendParametersGetData);

I set SendParametersGetData with the constructor function (in this example only ViewName and Parameters are set) and I give as input to the getMembers method.

ArrayList<Parameters> parameters = new ArrayList<>();
parameters.add(new Parameters("%FundId",String.valueOf(fund.getFundId())));
SendParametersGetData sendParametersGetData = new SendParametersGetData("Members_HousholdAdmin",parameters);
dataSource.getMembers(sendParametersGetData).subscribeOn(Schedulers.newThread())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new SingleObserver<ArrayList<Members>>() {
            @Override
            public void onSubscribe(Disposable d) {
                compositeDisposable.add(d);
            }

            @Override
            public void onSuccess(ArrayList<Members> members) {
            }

            @Override
            public void onError(Throwable e) {
                view.hideProgress();
                TastyToast.makeText(view.getContext(), e.toString(), 6000, TastyToast.ERROR);
            }
        });
1 Answers

I found the following error in your model class.

The Parameters needs to be renamed as parameters to match with the JSON key.

@SerializedName("parameters")
public List<Parameters> parameters = null;

This should solve your problem I guess. I think the other model classes are fine.

Related