RETROFIT 2.0 unbale to acess api响应数据包含任何列表

时间:2017-02-21 09:46:33

标签: java android retrofit2

嗨,大家好我在尝试访问一个api服务时遇到了一个问题,一切正常。但我的问题在于处理响应。

我正在使用改装2.0

以下是我的api的json回复

    {
  "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"
            }
          ]
        }
      ]
    }
  ]
}

以下是api调用代码

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;
    }
}

2 个答案:

答案 0 :(得分:1)

<强>解决方案

  1. 编辑Json structure
  2. 修改POJO课程(Datum
  3. 编辑Json结构

      

    data是一个对象,而不是数组。

    {
        "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"
                }]
            }]
        }
    }
    

    修改POJO课程

      

    转到下面的链接并再次生成pojo class

    http://www.jsonschema2pojo.org/

    试试这个。

    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();
    

答案 1 :(得分:-1)

如果您遇到问题,请将所有WorkTaskConfig替换为JsonElement。 并用Gson手动解析..

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