Android 改装2-应为BEGIN_数组,但为BEGIN_对象
我需要将json响应加载到微调器中。此代码用于其他活动。例如,我在first.class上加载了它,它可以工作,所以我将代码复制并粘贴到second.class,但它显示了一个错误“预期的BEGIN\u数组,但它是对象” 这是我的密码: Json响应:Android 改装2-应为BEGIN_数组,但为BEGIN_对象,android,json,api,retrofit,Android,Json,Api,Retrofit,我需要将json响应加载到微调器中。此代码用于其他活动。例如,我在first.class上加载了它,它可以工作,所以我将代码复制并粘贴到second.class,但它显示了一个错误“预期的BEGIN\u数组,但它是对象” 这是我的密码: Json响应: { "STATUS": "SUCCESS", "MESSAGE": "BOTTLE_SIZE", "DATA": [ { "ID_BOTTLE": "1", "NAME_BOTTLE": "350 ML",
{
"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;
}
}
公共类ApiInformationModel{
@序列化名称(“状态”)
@暴露
私有字符串成功;//对象
@SerializedName(“消息”)
@暴露
私有字符串消息;
@SerializedName(“数据”)
@暴露
私人名单数据;
公共字符串getSuccess(){
回归成功;
}
public void setSuccess(字符串成功){
成功=成功;
}
公共字符串getMessage(){
返回消息;
}
公共无效设置消息(字符串消息){
this.message=消息;
}
公共列表getData(){
返回数据;
}
公共无效设置数据(列表数据){
这个数据=数据;
}
公共类信息数据
{
//瓶子大小
@序列化名称(“ID_瓶”)
@暴露
私有字符串idBottle;
@序列化名称(“瓶名”)
@暴露
私人字符串名称瓶;
@序列化名称(“瓶值”)
@暴露
私有字符串值;
@序列化名称(“瓶子图像”)
@暴露
私有字符串图像;
公共字符串getIdBottle(){
返回idBottle;
}
公共void setIdBottle(字符串idBottle){
this.idBottle=idBottle;
}
公共字符串getnameball(){
归还姓名瓶;
}
公共void setnameball(字符串nameball){
this.nameball=nameball;
}
公共字符串getValue(){
返回值;
}
公共void setBottleValue(字符串值){
this.blottlevalue=瓶值;
}
公共字符串getImage(){
返回图像;
}
公共void setBottleImage(字符串图像){
this.blottleImage=瓶装图像;
}
}
我是这样称呼我的模型的:
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();
}
});
}
public void loadBottleSize(最终字符串bottleSizePosition){
informationInterface=ApiHandler.getApi(signupActivity.this).create(informationInterface.class);
调用signUpApi=informationInterface.getBlottleSize();
enqueue(新的回调函数(){
@凌驾
公共void onResponse(调用、响应){
试一试{
if(response.issusccessful()){
if(response.body().getSuccess().equals(“FAILED”)){
Toast.makeText(signupActivity.this,response.body().getMessage(),Toast.LENGTH_SHORT.show();
}
否则{
List stateItems=response.body().getData();
List listSpinner=new ArrayList();
瓶装水列表=新字符串[stateItems.size()];
对于(int i=0;i
提前感谢!这是因为您的数据是一个包含
信息数据列表的APInformationModel
对象,因此您必须更改:
List<ApiInformationModel.InformationData> stateItems = response.body().getData();
List stateItems=response.body().getData();
作者:
ApiInformationModel=response.body().getData();
List stateItems=model.getData();
我找到了答案,我只需要在界面上添加@Header。
ApiInformationModel model = response.body().getData();
List<ApiInformationModel.InformationData> stateItems = model.getData();