2016-05-17 37 views
-1

我是新的改造2.0 我有1個Ws,我得到這個迴應我已經使這個pojo使用http://www.jsonschema2pojo.org/但我無法得到qid和id。 雖然我越來越que和ans兩個,但都不是id的原因是什麼?android retrofit 2反應得到問題

和我越來越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" 
      }] 
     } 

    ] 
} 

響應我只是得到5月5日至十七日: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

請幫助我...

+0

從哪裏訪問'qid'和'id'?你有沒有嘗試打印或在一些變量中使用它? –

+0

什麼?我不明白你在說什麼? – Android

+0

你如何訪問qid和id? –

回答

1

你的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類。

enter image description here

+0

沒有屏幕截圖親切上傳 – Android

+0

我沒有收到你? – pRaNaY

1

你可以發佈你的響應代碼?

但是在方法onRespone中試試這個, response.body().getQue().getQid()

public class MyResponseBody{private Que que;} **闕爲對象,這樣你們需要做一個新的對象

public class Que{private String qid;}

PS:但get方法depense UR模式/ POJO。

+0

我已經張貼了我的響應代碼 「的考試」:[{ 「ANS」:[{ 「回答」: 「sdfdsrewwer」 },{ 「回答」: 「ewrewrewr」 },{ 「答案」: 「e」 的wrewrewr },{ 「答案」: 「e」 的wrewr 「 }], 」闕「:{ 」問題「:」 retreretř etret?「 } }, – Android

+0

sry只是讀它,但你已經得到了答案。如果你有問題,只需再次發佈^^ – Code21K

1

首先你的原始回答不正確。如果你粘貼你的迴應http://jsonlint.com/

你會在第12行得到解析錯誤。首先,修改它像

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

    ] 
} 

然後讓POJO上jsonschema2pojo工具。該工具將創建四個不同的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(); 
    } 
} 

希望它能工作。

+0

一旦檢查我的問題。這是類型mistack – Android

+0

@Android仍然你的迴應似乎是奇怪的無效。 – aquib23