我尝试从api下载我的数据
private List<Question> downloadQuestion;
Call<Questions> questionCall = MainApi.createService(MainService.class).
getQuestionsByChapIdAndType("15", 2);
questionCall.enqueue(new Callback<Questions>() {
@Override
public void onResponse(Response<Questions> response, Retrofit retrofit) {
downloadQuestion.addAll(response.body().getQuestions());
Log.d("Size",":"+downloadQuestion.size());
}
@Override
public void onFailure(Throwable t) {
Log.d("Size",t.toString());
}
});
但是这显示了此错误消息
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 28 path $.questions
帮我解决这个问题。 这是我的Question.java。
public class Question {
@SerializedName("chapter_id")
private int chapter_id;
@SerializedName("question_type_id")
private int question_type_id;
@SerializedName("question")
private String question;
@SerializedName("hint1")
private String hint1;
@SerializedName("hint2")
private String hint2;
@SerializedName("hint3")
private String hint3;
@SerializedName("hint4")
private String hint4;
@SerializedName("answer")
private String answer;
@SerializedName("year")
private int year;
public String getQuestion() {
return question;
}
public String getHint1() {
return hint1;
}
public String getHint2() {
return hint2;
}
public String getHint3() {
return hint3;
}
public String getHint4() {
return hint4;
}
public String getAnswer() {
return answer;
}
public int getChapter_id() {
return chapter_id;
}
public int getQuestion_type_id() {
return question_type_id;
}
public int getYear() {
return year;
}
public Question(String question, String hint1, String hint2, String hint3, String hint4, String answer) {
this.question = question;
this.hint1 = hint1;
this.hint2 = hint2;
this.hint3 = hint3;
this.hint4 = hint4;
this.answer = answer;
}
}
这是我的Questions.java。
public class Questions {
@SerializedName("questions")
private List<Question> questions;
public List<Question> getQuestions() {
return questions;
}
}
这是我的MainApi。
@GET("questions/{chapter_id}/{question_type_id}")
Call<Questions> getQuestionsByChapIdAndType(@Path("chapter_id") String chapter_id, @Path("question_type_id") int question_type_id);
我不知道为什么会这样。 请帮我。 我是一个非常新的android
答案 0 :(得分:0)
您的API返回Object,但在Questions.class中,您调用List&lt;问题&gt;。
public class Questions {
@SerializedName("questions")
private List<Question> questions;
public List<Question> getQuestions() {
return questions;
}
}
试试这个!!!
public class Questions {
@SerializedName("questions")
private Question questions;
}
}