android改造2响应越来越麻烦

时间:2016-05-17 09:49:32

标签: android retrofit2 jsonschema2pojo

我是Retrofit 2.0的新手 我有1 Ws,我得到这个回应我用这个使用http://www.jsonschema2pojo.org/制作pojo,但我无法得到qid和id。 虽然我得到了阙和ans两个但不是两者的id是什么原因?

我正在获得rawResponse也是不需要的部分。什么可以理由请帮助我。

{
    "paper": [{
            "que": {
                "qid": "1",
                "question": "????????????????"
            },
            "ans": [{
                "id": "1",
                "answer": "uiuyiyityu"
            }, {
                "id": "2",
                "answer": "ytrretwr etret"

            }, {
                "id": "3",
                "answer": "retre retret"
            }, {
                "id": "4",
                "answer": "rtretret"
            }]
        }

    ]
}

作为回应我刚收到05-17 05:43:03.380 4395-getFeed> =>:{

"exam": [{
        "ans": [{
            "answer": "sdfdsrewwer"
        }, {
            "answer": "ewrewrewr"
        }, {
            "answer": "e"wrewrewr
        }, {
            "answer": "e"wrewr"
        }],
        "que": {
            "question": "retreret retret?"
        }
    },

我创造了类似

的pojo

this img

我正在response.body().getExam().get(1).getQue().getQueId() = null

请帮助我...

3 个答案:

答案 0 :(得分:1)

你能发布你的回复代码吗?

但试试这个, 方法onRespone中的response.body().getQue().getQid()

public class MyResponseBody{private Que que;} ** Que是对象所以你需要创建一个新的Object

public class Que{private String qid;}

PS:但get方法依赖你的模型/ POJO。

答案 1 :(得分:1)

首先,您的原始回复不正确。如果您将回复粘贴到http://jsonlint.com/

你将在第12行得到解析错误。甚至jsonschema2pojo也在原始响应中显示错误。首先修改它像

{
  "paper": [
    {
      "que": {
        "qid": "1",
        "question": "????????????????"
      },
      "ans": [
        {
          "id": "1",
          "answer": "uiuyiyityu"
        },
        {
          "id": "2",
          "answer": "ytuyutyuty"
        },
        {
          "id": "3",
          "answer": "retre retret"
        },
        {
          "id": "4",
          "answer": "rtretret"
        }
      ]
    }

  ]
}

然后在jsonschema2pojo工具上制作POJO。该工具将创建四个不同的POJO,如下所示:

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class An {

@SerializedName("id")
@Expose
private String id;
@SerializedName("answer")
@Expose
private String answer;

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

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

/**
* 
* @return
* The answer
*/
public String getAnswer() {
return answer;
}

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

}

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

@Generated("org.jsonschema2pojo")
public class Paper {

@SerializedName("paper")
@Expose
private List<Paper_> paper = new ArrayList<Paper_>();

/**
* 
* @return
* The paper
*/
public List<Paper_> getPaper() {
return paper;
}

/**
* 
* @param paper
* The paper
*/
public void setPaper(List<Paper_> paper) {
this.paper = paper;
}

}

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

@Generated("org.jsonschema2pojo")
public class Paper_ {

@SerializedName("que")
@Expose
private Que que;
@SerializedName("ans")
@Expose
private List<An> ans = new ArrayList<An>();

/**
* 
* @return
* The que
*/
public Que getQue() {
return que;
}

/**
* 
* @param que
* The que
*/
public void setQue(Que que) {
this.que = que;
}

/**
* 
* @return
* The ans
*/
public List<An> getAns() {
return ans;
}

/**
* 
* @param ans
* The ans
*/
public void setAns(List<An> ans) {
this.ans = ans;
}

}

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Que {

@SerializedName("qid")
@Expose
private String qid;
@SerializedName("question")
@Expose
private String question;

/**
* 
* @return
* The qid
*/
public String getQid() {
return qid;
}

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

/**
* 
* @return
* The question
*/
public String getQuestion() {
return question;
}

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

}

现在,您可以通过调用POJO的方法轻松获取您的qid和id。使用此代码块获取qid和id。

Paper paper = new Paper();
List<Paper_> papers = paper.getPaper();

for(Paper_ paper_ : papers){
    int qid = paper_.getQue().getQid();
    List<An> answers = paper_.getAns();
    for(An answer : answers){
        int ansId = answer.getId();
    }
}

希望它能奏效。

答案 2 :(得分:1)

Your pojo class look like below:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ln-141 on 17/5/16.
 */
public class PaperClass {
    @SerializedName("paper")
    @Expose
    private List<Paper> paper = new ArrayList<Paper>();

    /**
     *
     * @return
     * The paper
     */
    public List<Paper> getPaper() {
        return paper;
    }

    /**
     *
     * @param paper
     * The paper
     */
    public void setPaper(List<Paper> paper) {
        this.paper = paper;
    }

    public class An {

        @SerializedName("id")
        @Expose
        private String id;
        @SerializedName("answer")
        @Expose
        private String answer;

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

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

        /**
         *
         * @return
         * The answer
         */
        public String getAnswer() {
            return answer;
        }

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

    }
    public class Paper {

        @SerializedName("que")
        @Expose
        private Que que;
        @SerializedName("ans")
        @Expose
        private List<An> ans = new ArrayList<An>();

        /**
         *
         * @return
         * The que
         */
        public Que getQue() {
            return que;
        }

        /**
         *
         * @param que
         * The que
         */
        public void setQue(Que que) {
            this.que = que;
        }

        /**
         *
         * @return
         * The ans
         */
        public List<An> getAns() {
            return ans;
        }

        /**
         *
         * @param ans
         * The ans
         */
        public void setAns(List<An> ans) {
            this.ans = ans;
        }

    }
    public class Que {

        @SerializedName("qid")
        @Expose
        private String qid;
        @SerializedName("question")
        @Expose
        private String question;

        /**
         *
         * @return
         * The qid
         */
        public String getQid() {
            return qid;
        }

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

        /**
         *
         * @return
         * The question
         */
        public String getQuestion() {
            return question;
        }

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

    }
}

Check selected option in below screenshot for creating pojo class.

enter image description here