I've viewed all the other stackoverflow topics regarding this, and they almost always have the same flaw in their JSON that is being called, which is that the JSON contains only one object, and not an array. I made sure to fix this, below you can see the JSON that is being returned after the query is executed in Postman.
I've exhausted my search for a solution, as every solution online involves converting the JSON into an array, like I have already done. I found a tutorial online on how to call a list using retrofit (Call<List<Object>>
) and it uses almost identical code to mine, and the JSON is in the same format, so I believe my JSON is correct
First, the JSON being returned:
[
{
"is_active": 1,
"match_date": "Wed, 09 Oct 2019 13:53:27 GMT",
"match_id": 2,
"match_winner_user_id": null,
"rnd1_cat1": "test category1",
"rnd1_cat1_q1": {
"category_id": 1,
"correct_answer": {
"answer_id": 1,
"answer_text": "test answer1",
"is_active": 1
},
"incorrect_answer1": {
"answer_id": 2,
"answer_text": "test answer2",
"is_active": 1
},
"incorrect_answer2": {
"answer_id": 3,
"answer_text": "test answer3",
"is_active": 1
},
"incorrect_answer3": {
"answer_id": 4,
"answer_text": "test answer4",
"is_active": 1
},
"is_active": 1,
"question_id": 1,
"question_text": "test question text1",
"user1_answer": 1,
"user2_answer": 2
},
"rnd1_cat1_q1_user1_answer": 1,
"rnd1_cat1_q1_user2_answer": 2,
"rnd1_cat1_q2": {
"category_id": 1,
"correct_answer": {
"answer_id": 4,
"answer_text": "test answer4",
"is_active": 1
},
"incorrect_answer1": {
"answer_id": 3,
"answer_text": "test answer3",
"is_active": 1
},
"incorrect_answer2": {
"answer_id": 2,
"answer_text": "test answer2",
"is_active": 1
},
"incorrect_answer3": {
"answer_id": 1,
"answer_text": "test answer1",
"is_active": 1
},
"is_active": 1,
"question_id": 2,
"question_text": "test question text2",
"user1_answer": 2,
"user2_answer": 2
},
"rnd1_cat1_q2_user1_answer": 2,
"rnd1_cat1_q2_user2_answer": 2,
"rnd1_cat1_q3": {
"category_id": 1,
"correct_answer": {
"answer_id": 3,
"answer_text": "test answer3",
"is_active": 1
},
"incorrect_answer1": {
"answer_id": 4,
"answer_text": "test answer4",
"is_active": 1
},
"incorrect_answer2": {
"answer_id": 2,
"answer_text": "test answer2",
"is_active": 1
},
"incorrect_answer3": {
"answer_id": 1,
"answer_text": "test answer1",
"is_active": 1
},
"is_active": 1,
"question_id": 3,
"question_text": "test question text3",
"user1_answer": 5,
"user2_answer": 4
},
"rnd1_cat1_q3_user1_answer": 5,
"rnd1_cat1_q3_user2_answer": 4,
"rnd1_cat2": "test category2",
"rnd1_cat2_q1": {
"category_id": 2,
"correct_answer": {
"answer_id": 6,
"answer_text": "test answer6",
"is_active": 1
},
"incorrect_answer1": {
"answer_id": 4,
"answer_text": "test answer4",
"is_active": 1
},
"incorrect_answer2": {
"answer_id": 2,
"answer_text": "test answer2",
"is_active": 1
},
"incorrect_answer3": {
"answer_id": 1,
"answer_text": "test answer1",
"is_active": 1
},
"is_active": 1,
"question_id": 4,
"question_text": "test question text4",
"user1_answer": null,
"user2_answer": null
},
"rnd1_cat2_q1_user1_answer": null,
"rnd1_cat2_q1_user2_answer": null,
"rnd1_cat2_q2": {
"category_id": 3,
"correct_answer": {
"answer_id": 6,
"answer_text": "test answer6",
"is_active": 1
},
"incorrect_answer1": {
"answer_id": 4,
"answer_text": "test answer4",
"is_active": 1
},
"incorrect_answer2": {
"answer_id": 2,
"answer_text": "test answer2",
"is_active": 1
},
"incorrect_answer3": {
"answer_id": 1,
"answer_text": "test answer1",
"is_active": 1
},
"is_active": 1,
"question_id": 5,
"question_text": "test question text5",
"user1_answer": null,
"user2_answer": null
},
"rnd1_cat2_q2_user1_answer": null,
"rnd1_cat2_q2_user2_answer": null,
"rnd1_cat2_q3": {
"category_id": 1,
"correct_answer": {
"answer_id": 2,
"answer_text": "test answer2",
"is_active": 1
},
"incorrect_answer1": {
"answer_id": 5,
"answer_text": "test answer5",
"is_active": 1
},
"incorrect_answer2": {
"answer_id": 4,
"answer_text": "test answer4",
"is_active": 1
},
"incorrect_answer3": {
"answer_id": 1,
"answer_text": "test answer1",
"is_active": 1
},
"is_active": 1,
"question_id": 6,
"question_text": "test question text6",
"user1_answer": null,
"user2_answer": null
},
"rnd1_cat2_q3_user1_answer": null,
"rnd1_cat2_q3_user2_answer": null,
"rnd2_cat1": null,
"rnd2_cat1_q1": null,
"rnd2_cat1_q1_user1_answer": null,
"rnd2_cat1_q1_user2_answer": null,
"rnd2_cat1_q2": null,
"rnd2_cat1_q2_user1_answer": null,
"rnd2_cat1_q2_user2_answer": null,
"rnd2_cat1_q3": null,
"rnd2_cat1_q3_user1_answer": null,
"rnd2_cat1_q3_user2_answer": null,
"rnd2_cat2": null,
"rnd2_cat2_q1": null,
"rnd2_cat2_q1_user1_answer": null,
"rnd2_cat2_q1_user2_answer": null,
"rnd2_cat2_q2": null,
"rnd2_cat2_q2_user1_answer": null,
"rnd2_cat2_q2_user2_answer": null,
"rnd2_cat2_q3": null,
"rnd2_cat2_q3_user1_answer": null,
"rnd2_cat2_q3_user2_answer": null,
"user1_id": 36,
"user2_id": 21
},
{
"is_active": 1,
"match_date": "Wed, 09 Oct 2019 13:53:27 GMT",
"match_id": 2,
"match_winner_user_id": null,
"rnd1_cat1": "test category1",
"rnd1_cat1_q1": {
"category_id": 1,
"correct_answer": {
"answer_id": 1,
"answer_text": "test answer1",
"is_active": 1
},
"incorrect_answer1": {
"answer_id": 2,
"answer_text": "test answer2",
"is_active": 1
},
"incorrect_answer2": {
"answer_id": 3,
"answer_text": "test answer3",
"is_active": 1
},
"incorrect_answer3": {
"answer_id": 4,
"answer_text": "test answer4",
"is_active": 1
},
"is_active": 1,
"question_id": 1,
"question_text": "test question text1",
"user1_answer": 1,
"user2_answer": 2
},
"rnd1_cat1_q1_user1_answer": 1,
"rnd1_cat1_q1_user2_answer": 2,
"rnd1_cat1_q2": {
"category_id": 1,
"correct_answer": {
"answer_id": 4,
"answer_text": "test answer4",
"is_active": 1
},
"incorrect_answer1": {
"answer_id": 3,
"answer_text": "test answer3",
"is_active": 1
},
"incorrect_answer2": {
"answer_id": 2,
"answer_text": "test answer2",
"is_active": 1
},
"incorrect_answer3": {
"answer_id": 1,
"answer_text": "test answer1",
"is_active": 1
},
"is_active": 1,
"question_id": 2,
"question_text": "test question text2",
"user1_answer": 2,
"user2_answer": 2
},
"rnd1_cat1_q2_user1_answer": 2,
"rnd1_cat1_q2_user2_answer": 2,
"rnd1_cat1_q3": {
"category_id": 1,
"correct_answer": {
"answer_id": 3,
"answer_text": "test answer3",
"is_active": 1
},
"incorrect_answer1": {
"answer_id": 4,
"answer_text": "test answer4",
"is_active": 1
},
"incorrect_answer2": {
"answer_id": 2,
"answer_text": "test answer2",
"is_active": 1
},
"incorrect_answer3": {
"answer_id": 1,
"answer_text": "test answer1",
"is_active": 1
},
"is_active": 1,
"question_id": 3,
"question_text": "test question text3",
"user1_answer": 5,
"user2_answer": 4
},
"rnd1_cat1_q3_user1_answer": 5,
"rnd1_cat1_q3_user2_answer": 4,
"rnd1_cat2": "test category2",
"rnd1_cat2_q1": {
"category_id": 2,
"correct_answer": {
"answer_id": 6,
"answer_text": "test answer6",
"is_active": 1
},
"incorrect_answer1": {
"answer_id": 4,
"answer_text": "test answer4",
"is_active": 1
},
"incorrect_answer2": {
"answer_id": 2,
"answer_text": "test answer2",
"is_active": 1
},
"incorrect_answer3": {
"answer_id": 1,
"answer_text": "test answer1",
"is_active": 1
},
"is_active": 1,
"question_id": 4,
"question_text": "test question text4",
"user1_answer": null,
"user2_answer": null
},
"rnd1_cat2_q1_user1_answer": null,
"rnd1_cat2_q1_user2_answer": null,
"rnd1_cat2_q2": {
"category_id": 3,
"correct_answer": {
"answer_id": 6,
"answer_text": "test answer6",
"is_active": 1
},
"incorrect_answer1": {
"answer_id": 4,
"answer_text": "test answer4",
"is_active": 1
},
"incorrect_answer2": {
"answer_id": 2,
"answer_text": "test answer2",
"is_active": 1
},
"incorrect_answer3": {
"answer_id": 1,
"answer_text": "test answer1",
"is_active": 1
},
"is_active": 1,
"question_id": 5,
"question_text": "test question text5",
"user1_answer": null,
"user2_answer": null
},
"rnd1_cat2_q2_user1_answer": null,
"rnd1_cat2_q2_user2_answer": null,
"rnd1_cat2_q3": {
"category_id": 1,
"correct_answer": {
"answer_id": 2,
"answer_text": "test answer2",
"is_active": 1
},
"incorrect_answer1": {
"answer_id": 5,
"answer_text": "test answer5",
"is_active": 1
},
"incorrect_answer2": {
"answer_id": 4,
"answer_text": "test answer4",
"is_active": 1
},
"incorrect_answer3": {
"answer_id": 1,
"answer_text": "test answer1",
"is_active": 1
},
"is_active": 1,
"question_id": 6,
"question_text": "test question text6",
"user1_answer": null,
"user2_answer": null
},
"rnd1_cat2_q3_user1_answer": null,
"rnd1_cat2_q3_user2_answer": null,
"rnd2_cat1": null,
"rnd2_cat1_q1": null,
"rnd2_cat1_q1_user1_answer": null,
"rnd2_cat1_q1_user2_answer": null,
"rnd2_cat1_q2": null,
"rnd2_cat1_q2_user1_answer": null,
"rnd2_cat1_q2_user2_answer": null,
"rnd2_cat1_q3": null,
"rnd2_cat1_q3_user1_answer": null,
"rnd2_cat1_q3_user2_answer": null,
"rnd2_cat2": null,
"rnd2_cat2_q1": null,
"rnd2_cat2_q1_user1_answer": null,
"rnd2_cat2_q1_user2_answer": null,
"rnd2_cat2_q2": null,
"rnd2_cat2_q2_user1_answer": null,
"rnd2_cat2_q2_user2_answer": null,
"rnd2_cat2_q3": null,
"rnd2_cat2_q3_user1_answer": null,
"rnd2_cat2_q3_user2_answer": null,
"user1_id": 36,
"user2_id": 21
}
]
and the interface class:
@POST("/api/v1/match/getMatches")
Call<List<Match>> getMatches(@Header("Authorization") String authorization);
the Match class, which is just Annotating the objects and auto-generated getters:
import com.google.gson.annotations.SerializedName;
public class Match {
@SerializedName("match_id")
private int matchId;
@SerializedName("user1_id")
private int user1Id;
@SerializedName("user2_id")
private int user2Id;
// Categories
@SerializedName("rnd1_cat1")
private String rnd1_cat1;
@SerializedName("rnd1_cat2")
private String rnd1_cat2;
@SerializedName("rnd2_cat1")
private String rnd2_cat1;
@SerializedName("rnd2_cat2")
private String rnd2_cat2;
//Questions, which contain answers objects within them
@SerializedName("rnd1_cat1_q1")
private Question rnd1_cat1_q1;
@SerializedName("rnd1_cat1_q2")
private Question rnd1_cat1_q2;
@SerializedName("rnd1_cat1_q3")
private Question rnd1_cat1_q3;
//Cat2
@SerializedName("rnd1_cat2_q1")
private Question rnd1_cat2_q1;
@SerializedName("rnd1_cat2_q2")
private Question rnd1_cat2_q2;
@SerializedName("rnd1_cat2_q3")
private Question rnd1_cat2_q3;
//Round 2
@SerializedName("rnd2_cat1_q1")
private Question rnd2_cat1_q1;
@SerializedName("rnd2_cat1_q2")
private Question rnd2_cat1_q2;
@SerializedName("rnd2_cat1_q3")
private Question rnd2_cat1_q3;
//Cat2
@SerializedName("rnd2_cat2_q1")
private Question rnd2_cat2_q1;
@SerializedName("rnd2_cat2_q2")
private Question rnd2_cat2_q2;
@SerializedName("rnd2_cat2_q3")
private Question rnd2_cat2_q3;
public int getMatchId() {
return matchId;
}
public int getUser1Id() {
return user1Id;
}
public int getUser2Id() {
return user2Id;
}
public String getRnd1_cat1() {
return rnd1_cat1;
}
public String getRnd1_cat2() {
return rnd1_cat2;
}
public String getRnd2_cat1() {
return rnd2_cat1;
}
public String getRnd2_cat2() {
return rnd2_cat2;
}
public Question getRnd1_cat1_q1() {
return rnd1_cat1_q1;
}
public Question getRnd1_cat1_q2() {
return rnd1_cat1_q2;
}
public Question getRnd1_cat1_q3() {
return rnd1_cat1_q3;
}
public Question getRnd1_cat2_q1() {
return rnd1_cat2_q1;
}
public Question getRnd1_cat2_q2() {
return rnd1_cat2_q2;
}
public Question getRnd1_cat2_q3() {
return rnd1_cat2_q3;
}
public Question getRnd2_cat1_q1() {
return rnd2_cat1_q1;
}
public Question getRnd2_cat1_q2() {
return rnd2_cat1_q2;
}
public Question getRnd2_cat1_q3() {
return rnd2_cat1_q3;
}
public Question getRnd2_cat2_q1() {
return rnd2_cat2_q1;
}
public Question getRnd2_cat2_q2() {
return rnd2_cat2_q2;
}
public Question getRnd2_cat2_q3() {
return rnd2_cat2_q3;
}
}
the Question class:
import com.google.gson.annotations.SerializedName;
public class Question {
@SerializedName("question_id")
private int questionId;
@SerializedName("category_name")
private String category;
@SerializedName("question_text")
private String questionText;
@SerializedName("correct_answer")
private Answer correctAnswer;
@SerializedName("incorrect_answer1")
private Answer incorrectAnswer1;
@SerializedName("incorrect_answer2")
private Answer incorrectAnswer2;
@SerializedName("incorrect_answer3")
private Answer incorrectAnswer3;
public int getQuestionId() {
return questionId;
}
public String getCategory() {
return category;
}
public String getQuestionText() {
return questionText;
}
public Answer getCorrectAnswer() {
return correctAnswer;
}
public Answer getIncorrectAnswer1() {
return incorrectAnswer1;
}
public Answer getIncorrectAnswer2() {
return incorrectAnswer2;
}
public Answer getIncorrectAnswer3() {
return incorrectAnswer3;
}
}
and the Answer class:
import com.google.gson.annotations.SerializedName;
public class Answer {
@SerializedName("answer_id")
private int answerId;
@SerializedName("answer_text")
private String answerText;
public int getAnswerId() {
return answerId;
}
public String getAnswerText() {
return answerText;
}
}
and finally the call:
Call<List<Match>> call = backendApi.getMatches(auth_token);
call.enqueue(new Callback<List<Match>>() {
@Override
public void onResponse(Call<List<Match>> call, Response<List<Match>> response) {
if(!response.isSuccessful()){
Log.e("login activity", "2");
return;
}
List<Match> matchList = response.body();
Log.i("match list", matchList.size()+"");
}
@Override
public void onFailure(Call<List<Match>> call, Throwable t) {
Log.e("retrofit", t.getMessage());
}
});
For some reason, I get the error: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path, even though everything is in order as far as I can tell.
Any help is greatly appreciated, thanks!
EDIT: I seem to notice that Postman and Retrofit think the Content-Lengths are different in the header. Postman sees the true value, which is 5000+ for just one match in the json, and Retrofit returns a Content-Length of 64 no matter what. Weird.