【发布时间】:2017-08-26 04:44:06
【问题描述】:
我是 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
请帮帮我。
【问题讨论】:
-
在 POSTMAN 中检查您的 API。它必须来自一个不在数组中的对象。如错误所述。
-
正如我已经提到的。它是 json 数组而不是里面的标准 json 对象
-
你会分享 API url 吗?
-
由于隐私政策,我不能。
标签: java android json retrofit retrofit2