-1

I am new to Retrofit 2.0 i have 1 Ws in to that i am getting this response i have make pojo for this using http://www.jsonschema2pojo.org/ but i am not able to get qid and id. though i am getting que and ans both but not id of both what can the reason?

and i am getting rawResponse also that is unwanted part. what can be reason please help me out.

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

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

    ]
}
   

in response i just get 05-17 05:43:03.380 4395-getFeed > =>: {

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

I have created pojo like

this img

i am getting response.body().getExam().get(1).getQue().getQueId() = null

kindly help me out...

Community
  • 1
  • 1
Android
  • 8,995
  • 9
  • 67
  • 108

3 Answers3

1

Can you post your response code??

But try this, response.body().getQue().getQid() in method onRespone.

public class MyResponseBody{private Que que;} **Que is object so u need make a new Object

public class Que{private String qid;}

PS : but the get method depense ur model/POJO.

Code21K
  • 74
  • 2
  • i have posted my response code "exam": [{ "ans": [{ "answer": "sdfdsrewwer" }, { "answer": "ewrewrewr" }, { "answer": "e"wrewrewr }, { "answer": "e"wrewr" }], "que": { "question": "retreret retret?" } }, – Android May 17 '16 at 10:03
  • sry just read it, but you have got the answer. If you have an issue just post it again ^^ – Code21K May 18 '16 at 01:35
1

First of all your raw response is incorrect. If you paste your response in http://jsonlint.com/

You will get parse error in line 12. Even jsonschema2pojo is also displaying error in your raw response. First of all modify it like

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

  ]
}

Then make POJO on jsonschema2pojo tool. The tool will create four different POJOs like following:

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

}

Now you can get your qid and id very easily by calling methods of your POJOs. Use this code block for getting qid and 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();
    }
}

Hope it will work.

aquib
  • 194
  • 2
  • 13
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

pRaNaY
  • 24,642
  • 24
  • 96
  • 146