我已经查看了所有其他与此有关的stackoverflow主题,并且它们在被调用的JSON中几乎总是存在相同的缺陷,即JSON仅包含一个对象,而不包含数组。我已确定要解决此问题,在下面您可以看到在Postman中执行查询后返回的JSON。
我已经用尽了所有的解决方案,因为每个在线解决方案都涉及将JSON转换为数组,就像我已经做过的那样。我在网上找到了有关如何使用改型(Call<List<Object>>
来调用列表的教程,该教程使用几乎相同的代码进行挖掘,并且JSON的格式相同,所以我认为我的JSON是正确的
首先,返回JSON:
[
{
"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
},
(snipped part of JSON out, it's about 450 lines in total),
"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
},
(snipped part of JSON out, it's about 450 lines in total),
"rnd2_cat2_q3_user1_answer": null,
"rnd2_cat2_q3_user2_answer": null,
"user1_id": 36,
"user2_id": 21
}
]
和接口类:
@POST("/api/v1/match/getMatches")
Call<List<Match>> getMatches(@Header("Authorization") String authorization);
Match类,仅用于注释对象和自动生成的吸气剂:
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;
}
}
最后是电话:
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());
}
});
由于某种原因,我得到了一个错误:预期是BEGIN_ARRAY,但在第1行第2列路径处是BEGIN_OBJECT,尽管据我所知一切正常。
非常感谢您的帮助!