简体   繁体   中英

RETROFIT 2.0 unbale to acess api response data contains any list

Hi guys i have struck with one problem while i am trying to access one api services it is all working fine. But my issue iin processing the response .

I am using retrofit 2.0

Below is my json response for my api

    {
  "status": 200,
  "success": "true",
  "data": [
    {
      "works_node": [
        {
          "works_items": [
            {
              "work_id": "number",

              "preference": "number",
              "Task_created_time": "datetime yyyy-mm-dd h:m:s"
            }
          ]
        }
      ],
      "questions_node": [
        {
          "questions_items": [
            {
              "q_id": "number",
              "work_id": "number",
              "question_text": "string",
              "preference": "number" 
            }
          ]
        }
      ],
      "answers_node": [
        {
          "answers_items": [
            {
              "a_id": "number",
              "q_id": "number",
              "answer_text": "string",
              "prefernce": "number",
              "point": "number",
              "is_suggest": "number",
              "work_id": "number"
            }
          ]
        }
      ],
      "answer_suggestions_node": [
        {
          "answer_suggestions_items": [
            {
              "a_id": "number",
              "q_id": "number",
              "answer_suggestion_text": "string",
              "point": "number",
              "work_id": "number"
            }
          ]
        }
      ]
    }
  ]
}

Below is the api calling code

public  void getWorkTaskConfig(){
    ApiInterface apiService =  ApiClient.getClient().create(ApiInterface.class);
    String DeviceImei=uf.getIMEI();
    int userId=db.getUserId();

    Call<WorkTaskConfig> call = apiService.getWorkTaskConfig
            (new BasicData(new UserInfo(userId),new DeviceInfo(DeviceImei)));
    call.enqueue(new Callback<WorkTaskConfig>() {
        @Override
        public void onResponse(Call<WorkTaskConfig> call, Response<WorkTaskConfig> response) {
            try {
                int apiStatus= response.body().getStatus();
                Boolean apiSuccess=response.body().getSuccess();
                if (apiStatus == Constants.RESULT_CODE_OK &&
                        apiSuccess.equals(Constants.RESULT_SUCCESS_OK)) {     //data received successfully
                    List<Datum> apiAllWorkTaskConfigData=response.body().getData();
                    List<WorksNode> apiAllWorksNodeData=response.body().getData().getWorksNode();    // im facing 
                    //the problem here ie i am not able to access the getWorksNode() function 
                    //in Datum.java class. 
                }else{                  //while retrieving data something went wrong.  
                }
            } catch (Exception e) { e.printStackTrace(); }
        } 
        @Override
        public void onFailure(Call<WorkTaskConfig> call, Throwable t) {   }
    }); 
}

WorkTaskConfig.java

public class WorkTaskConfig {
    @SerializedName("status")
    @Expose
    private Integer status;

    @SerializedName("success")
    @Expose
    private Boolean success;

    @SerializedName("data")
    @Expose
    private List<Datum> data = null;

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public List<Datum> getData() {
        return data;
    }

    public void setData(List<Datum> data) {
        this.data = data;
    }
}

Datum.java

public class Datum {

    @SerializedName("works_node")
    @Expose
    private List<WorksNode> worksNode = null;

    public List<WorksNode> getWorksNode() {
        return worksNode;
    }

    public void setWorksNode(List<WorksNode> worksNode) {
        this.worksNode = worksNode;
    }
}

Solution

  1. Edit Json structure
  2. Edit POJO class( Datum )

Edit Json Structure

data is an object, not array.

{
    "status": 200,
    "success": "true",
    "data": {
        "works_node": [{
            "works_items": [{
                "work_id": "number",
                "preference": "number",
                "Task_created_time": "datetime yyyy-mm-dd h:m:s"
            }]
        }],
        "questions_node": [{
            "questions_items": [{
                "q_id": "number",
                "work_id": "number",
                "question_text": "string",
                "preference": "number"
            }]
        }],
        "answers_node": [{
            "answers_items": [{
                "a_id": "number",
                "q_id": "number",
                "answer_text": "string",
                "prefernce": "number",
                "point": "number",
                "is_suggest": "number",
                "work_id": "number"
            }]
        }],
        "answer_suggestions_node": [{
            "answer_suggestions_items": [{
                "a_id": "number",
                "q_id": "number",
                "answer_suggestion_text": "string",
                "point": "number",
                "work_id": "number"
            }]
        }]
    }
}

Edit POJO class

go to link below and generate the pojo class again.

http://www.jsonschema2pojo.org/

And Try this.

WorkTaskConfig taskConfig = reponse.body();

List<WorksNode> worksNode = taskConfig.getData().getWorksNode();
List<QuestionsNode> questionsNode = taskConfig.getData().getQuestionsNode();
List<AnswersNode> answersNode = taskConfig.getData().getAnswersNode();
List<AnswerSuggestionsNode> answerSuggestionsNode = taskConfig.getData().getAnswerSuggestionsNode();

Replace all WorkTaskConfig to JsonElement if you are facing some problem . and Parse manually with Gson like..

String apiAllWorkTaskConfigData = response.body().getData(); WorkTaskConfig mWorkTaskConfig = new Gson.fromJson(apiAllworkTaskConfigData,WorkTaskConfig.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM