1

I have api with all request like:

{
  "success": "true",
  "data": [],
  "errors": []
}

For example:

{
  "success": "true",
  "data": [
             {
                 "id": "666",
                 "name": "Cars",
                 "imgUrl": "/images/mod_catalog_prod/categories/no-image.jpg",
                 "descr": "",
                 "childRubricCount": 20
            },
            {
                 "id": "667",
                 "name": "Buses",
                 "imgUrl": "/images/mod_catalog_prod/categories/no-image.jpg",
                 "descr": "",
                 "childRubricCount": 14
           }
  ],
  "errors": []
}

Also if success==true is possible call onResponse block and if success==false call onFailure block with error text from errors variable from json. Api contains different response data models for data block in json response and i need that data model class would be dynamically changed. Is there the way to create something like this? All responses will be useful for me.

couldDog
  • 182
  • 2
  • 12
  • 1
    you form data JsonArray Like this , your json data format show error. "data": [ { "id": "666", "name": "Cars", "imgUrl": "/images/mod_catalog_prod/categories/no-image.jpg", "descr": "", "childRubricCount": 20 }, { "id": "667", "name": "Buses", "imgUrl": "/images/mod_catalog_prod/categories/no-image.jpg", "descr": "", "childRubricCount": 14 } ] – Jai Rajesh Mar 21 '16 at 11:44
  • 1
    you need to make same type of response for every case make your error of String type. Make Model only for Success type. – kundan roy Mar 21 '16 at 11:59
  • @Jai Rajesh You right, i've corrected. But question is what is true way to handle data for different java class models from data json field. And throw exception with error message if success == false. – couldDog Mar 21 '16 at 12:01
  • @Kundan Kumar Roy So I did, but I think there is a way that allows to do it on the side of retrofit / jackson. For example this is same questions http://stackoverflow.com/questions/23070298/get-nested-json-object-with-gson-using-retrofit?rq=1 this applies to GSON. – couldDog Mar 21 '16 at 12:06
  • Are you using retrofit? – Jai Rajesh Mar 21 '16 at 12:10

1 Answers1

2

Try this

Add this compile 'com.google.code.gson:gson:2.6.2' dependencies in your gradle file.

After getting your response jsonObject Pass like this

public void onRequestSuccess(JSONObject jsonObject) {
    ModelItem item = new Gson().fromJson(jsonObject.toString(), ModelItem.class);
    List<ModelItem.DataItem> dataItems;
    List<ModelItem.ErrorItem> errorItems;

    if (item.getSuccess().equalsIgnoreCase("true")) {
        dataItems = item.getData();
        for (int i = 0; i < dataItems.size(); i++) {
            String name = dataItems.get(i).getName();
        }
    } else {
        errorItems = item.getErrors();
        for (int i = 0; i < errorItems.size(); i++) {
            String name = errorItems.get(i).getErrorMsg();
        }
    }
}

Create Pojo class ModelItem.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;

public class ModelItem {

@SerializedName("success")
@Expose
private String success;
@SerializedName("data")
@Expose
private List<DataItem> data = new ArrayList<DataItem>();
@SerializedName("errors")
@Expose
private List<ErrorItem> errors = new ArrayList<ErrorItem>();

/**
 * @return The success
 */
public String getSuccess() {
    return success;
}

/**
 * @param success The success
 */
public void setSuccess(String success) {
    this.success = success;
}

/**
 * @return The data
 */
public List<DataItem> getData() {
    return data;
}

/**
 * @param data The data
 */
public void setData(List<DataItem> data) {
    this.data = data;
}

/**
 * @return The errors
 */
public List<ErrorItem> getErrors() {
    return errors;
}

/**
 * @param errors The errors
 */
public void setErrors(List<ErrorItem> errors) {
    this.errors = errors;
}

public class DataItem {

    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("imgUrl")
    @Expose
    private String imgUrl;
    @SerializedName("descr")
    @Expose
    private String descr;
    @SerializedName("childRubricCount")
    @Expose
    private Integer childRubricCount;

    /**
     * @return The id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id The id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return The name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name The name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return The imgUrl
     */
    public String getImgUrl() {
        return imgUrl;
    }

    /**
     * @param imgUrl The imgUrl
     */
    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    /**
     * @return The descr
     */
    public String getDescr() {
        return descr;
    }

    /**
     * @param descr The descr
     */
    public void setDescr(String descr) {
        this.descr = descr;
    }

    /**
     * @return The childRubricCount
     */
    public Integer getChildRubricCount() {
        return childRubricCount;
    }

    /**
     * @param childRubricCount The childRubricCount
     */
    public void setChildRubricCount(Integer childRubricCount) {
        this.childRubricCount = childRubricCount;
    }
}

public class ErrorItem {

    @SerializedName("error_code")
    @Expose
    private String errorCode;
    @SerializedName("error_msg")
    @Expose
    private String errorMsg;

    /**
     * @return The errorCode
     */
    public String getErrorCode() {
        return errorCode;
    }

    /**
     * @param errorCode The error_code
     */
    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    /**
     * @return The errorMsg
     */
    public String getErrorMsg() {
        return errorMsg;
    }

    /**
     * @param errorMsg The error_msg
     */
    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }
}
}
Jai Rajesh
  • 939
  • 5
  • 18
  • 1
    Your error json array contain empty list. I add error list like this "errors": [ { "error_code": "200", "error_msg": "Cars" } ] you need change what you want here and add the key parameter in Model class create getter and setter method. – Jai Rajesh Mar 21 '16 at 12:03