0

I tried to download my data from api like this

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

But this show this error message

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 28 path $.questions

Help me to solve this problem. This is my 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;
}
}

and this is my Questions.java.

public class Questions {

@SerializedName("questions")
private List<Question> questions;

public List<Question> getQuestions() {
    return questions;
}
}

This is my 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);

MyApi Image

I dont know why this is happening. please help me. i m very new to android

Min Khant Lu
  • 712
  • 1
  • 9
  • 20
  • Possible duplicate of [GSON throwing "Expected BEGIN\_OBJECT but was BEGIN\_ARRAY"?](http://stackoverflow.com/questions/9598707/gson-throwing-expected-begin-object-but-was-begin-array) – Robert May 15 '16 at 04:41

1 Answers1

0

Your API return Object, But in Questions.class you call List< Questions >.

public class Questions {

@SerializedName("questions")
private List<Question> questions;

public List<Question> getQuestions() {
return questions;
}
}

Try this !!!

public class Questions {

@SerializedName("questions")
private Question questions;

}
}
Nguyễn Trung Hiếu
  • 2,004
  • 1
  • 10
  • 22