繁体   English   中英

Android Retrofit 2响应越来越麻烦

[英]android retrofit 2 response getting trouble

我是Retrofit 2.0的新手,我对此有1 Ws的响应,我已经使用http://www.jsonschema2pojo.org/为此进行了pojo,但我无法获得qid和id。 尽管我同时获得了que和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?"
        }
    },

我创造了像

这个img

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

请帮我...

您可以发布您的回复代码吗?

但是尝试一下response.body().getQue().getQid()方法中的response.body().getQue().getQid()

public class MyResponseBody{private Que que;} ** public class MyResponseBody{private Que que;}是对象,因此您需要创建一个新对象

public class Que{private String qid;}

PS:但是get方法取决于您的模型/ POJO。

首先,您的原始响应不正确。 如果您将回复粘贴到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();
    }
}

希望它能工作。

您的pojo类如下所示:

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

    }
}

检查下面的屏幕截图中的选定选项以创建pojo类。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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