java.lang.IllegalStateException:预期BEGIN_OBJECT但在第1行第2行路径$ BEGIN_ARRAY

时间:2017-08-26 04:44:06

标签: java android json retrofit retrofit2

我是Retrofit的新手。以下是获取供应商列表的代码

示例JSON响应是json数组&不是json对象

[
{
"Key" : "1",
"Value" : "xyz"
},
{
"Key" : "2",
"Value" : "abc"
}
]

以下是我的代码

private void callToRetrofit() {
    ApiInterface apiInterface = ApiClient.getApiClient()
            .create(ApiInterface.class);
    Call<List<SD_Checklist_Supplier_Model>> call = apiInterface.getVendors();
    call.enqueue(new Callback<List<SD_Checklist_Supplier_Model>>() {
        @Override
        public void onResponse(Call<List<SD_Checklist_Supplier_Model>> call, Response<List<SD_Checklist_Supplier_Model>> response) {
            Log.v("onResponse", " : ok" +  response == null ? " null" : "okkkk" + response.toString());
        }

        @Override
        public void onFailure(Call<List<SD_Checklist_Supplier_Model>> call, Throwable t) {
            Log.v("onFailure", " : " + t.toString());
        }
    });
}

模型类

public class SD_Checklist_Supplier_Model {

    @SerializedName("Key")
    private String supplierID;
    @SerializedName("Value")
    private String supplierName;
    private boolean isSupplierSelected;

    public String getSupplierID() {
        return supplierID;
    }

    public void setSupplierID(String supplierID) {
        this.supplierID = supplierID;
    }

    public String getSupplierName() {
        return supplierName;
    }

    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }

    public boolean isSupplierSelected() {
        return isSupplierSelected;
    }

    public void setSupplierSelected(boolean supplierSelected) {
        isSupplierSelected = supplierSelected;
    }
}

目前,我正在

  

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:   预期BEGIN_OBJECT但在第1行第2列路径$

处是BEGIN_ARRAY

请帮帮我。

1 个答案:

答案 0 :(得分:0)

您的API调用响应是一个列表,您没有分配给列表。

更改您的代码

 Call<SD_Checklist_Supplier_Model> call = apiInterface.getVendors();

作为

 Call<List<SD_Checklist_Supplier_Model>> call = apiInterface.getVendors();