当前位置: 首页 > 知识库问答 >
问题:

java.lang.IllegalStateException:应为BEGIN_OBJECT,但在第1行第2列路径$处为BEGIN_ARRAY

欧阳玺
2023-03-14
[
{
"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个答案

史弘博
2023-03-14

API调用的响应是一个列表,而不是分配给列表。

更改代码

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

作为

 Call<List<SD_Checklist_Supplier_Model>> call = apiInterface.getVendors();
 类似资料: