我需要将json响应加载到微调器中。此代码可用于其他活动。例如,我将它加载到first.class上并且可以正常工作,因此我将代码复制并粘贴到second.class上,但显示错误“ EXPECTED BEGIN_ARRAY BUT WAS OBJECT”
这是我的代码:
Json的回复:
{
"STATUS": "SUCCESS",
"MESSAGE": "BOTTLE_SIZE",
"DATA": [
{
"ID_BOTTLE": "1",
"NAME_BOTTLE": "350 ML",
"BOTTLE_VALUE": "350",
"BOTTLE_IMAGE": "http://128.199.120.151/admin_refil/media/bottle/WhatsApp_Image_2018-08-30_at_3.51_.42_PM_.jpeg"
},
{
"ID_BOTTLE": "2",
"NAME_BOTTLE": "500 ML",
"BOTTLE_VALUE": "500",
"BOTTLE_IMAGE": "http://128.199.120.151/admin_refil/media/bottle/WhatsApp_Image_2018-08-30_at_3.51_.45_PM_.jpeg"
},
{
"ID_BOTTLE": "3",
"NAME_BOTTLE": "750 ML",
"BOTTLE_VALUE": "750",
"BOTTLE_IMAGE": "http://128.199.120.151/admin_refil/media/bottle/WhatsApp_Image_2018-08-30_at_3.51_.44_PM_.jpeg"
},
{
"ID_BOTTLE": "4",
"NAME_BOTTLE": "1 Liter",
"BOTTLE_VALUE": "1000",
"BOTTLE_IMAGE": "http://128.199.120.151/admin_refil/media/bottle/WhatsApp_Image_2018-08-30_at_3.51_.43_PM_.jpeg"
}
]
}
Json pojo:
public class ApiInformationModel {
@SerializedName("STATUS")
@Expose
private String success; //object
@SerializedName("MESSAGE")
@Expose
private String message;
@SerializedName("DATA")
@Expose
private List<InformationData> data;
public String getSuccess() {
return success;
}
public void setSuccess(String success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<InformationData> getData() {
return data;
}
public void setData(List<InformationData> data) {
this.data = data;
}
public class InformationData
{
//bottle Size
@SerializedName("ID_BOTTLE")
@Expose
private String idBottle;
@SerializedName("NAME_BOTTLE")
@Expose
private String namebottle;
@SerializedName("BOTTLE_VALUE")
@Expose
private String bottleValue;
@SerializedName("BOTTLE_IMAGE")
@Expose
private String bottleImage;
public String getIdBottle() {
return idBottle;
}
public void setIdBottle(String idBottle) {
this.idBottle = idBottle;
}
public String getNamebottle() {
return namebottle;
}
public void setNamebottle(String namebottle) {
this.namebottle = namebottle;
}
public String getBottleValue() {
return bottleValue;
}
public void setBottleValue(String bottleValue) {
this.bottleValue = bottleValue;
}
public String getBottleImage() {
return bottleImage;
}
public void setBottleImage(String bottleImage) {
this.bottleImage = bottleImage;
}
}
这就是我称呼我的模型的方式:
public void loadBottleSize(final String bottleSizePosition) {
informationInterface = ApiHandler.getApi(signupActivity.this).create(InformationInterface.class);
Call<ApiInformationModel> signUpApi = informationInterface.getBottleSize();
signUpApi.enqueue(new Callback<ApiInformationModel>() {
@Override
public void onResponse(Call<ApiInformationModel> call, Response<ApiInformationModel> response) {
try {
if (response.isSuccessful()) {
if (response.body().getSuccess().equals("FAILED")) {
Toast.makeText(signupActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
}
else {
List<ApiInformationModel.InformationData> stateItems = response.body().getData();
List<String> listSpinner = new ArrayList<String>();
bottleList = new String[stateItems.size()];
for (int i = 0; i < stateItems.size(); i++){
bottleList[i] = stateItems.get(i).getBottleValue();
listSpinner.add(stateItems.get(i).getNamebottle());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(signupActivity.this,
android.R.layout.simple_spinner_item, listSpinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
bottleSize.setAdapter(adapter);
}
} else {
try {
Converter<ResponseBody, ApiError> converter = ApiHandler.getApi(signupActivity.this).responseBodyConverter(ApiError.class, new Annotation[0]);
ApiError error = converter.convert(response.errorBody());
Toast.makeText(signupActivity.this, "ini errornya " + error.getMessage(), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(signupActivity.this, response.errorBody().string(), Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
Toast.makeText(signupActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ApiInformationModel> call, Throwable t) {
Toast.makeText(signupActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
谢谢!
答案 0 :(得分:0)
这是因为您的数据是一个ApiInformationModel
对象,其中包含InformationData
的列表,因此您必须进行以下更改:
List<ApiInformationModel.InformationData> stateItems = response.body().getData();
作者:
ApiInformationModel model = response.body().getData();
List<ApiInformationModel.InformationData> stateItems = model.getData();