我已经开始在android中学习Api消费。我使用了改造但是我收到了错误。我有以下Api响应
{
"matches": [
{
"unique_id": 1122277,
"date": "2018-01-13T00:00:00.000Z",
"team-2": "India",
"team-1": "South Africa",
"type": "Test",
"dateTimeGMT": "2018-01-13T08:00:00.000Z",
"squad": true,
"toss_winner_team": "South Africa",
"matchStarted": true
},
{
"unique_id": 1116902,
"team-2": "Namibia Under-19s",
"team-1": "England Under-19s",
"type": "YouthODI",
"date": "2018-01-14T00:00:00.000Z",
"dateTimeGMT": "2018-01-14T21:30:00.000Z",
"squad": true,
"toss_winner_team": "Namibia Under-19s",
"winner_team": "England Under-19s",
"matchStarted": true
}
],
"v": "1",
"ttl": 68,
"provider": {
"source": "Various",
"url": "https://cricapi.com/",
"pubDate": "2018-01-16T16:28:19.313Z"
},
"creditsLeft": 250
}
我有以下代码
模型类
public class NewMatchList {
List<MatchList> matchList;
public NewMatchList(List<MatchList> matchList) {
this.matchList = matchList;
}
public List<MatchList> getMatchList() {
return matchList;
}
public void setMatchList(List<MatchList> matchList) {
this.matchList = matchList;
}
@Override
public String toString() {
return super.toString();
}
public class MatchList{
private int id;
private String date;
private String teamOne;
private String teamTwo;
private String matchType;
private String dateTimeGMT;
private boolean squad;
private String tossWinnerTeam;
private String winnerTeam;
private boolean matchStarted;
public MatchList(int id, String date, String teamOne, String teamTwo, String matchType, String dateTimeGMT, boolean squad, String tossWinnerTeam, String winnerTeam, boolean matchStarted) {
this.id = id;
this.date = date;
this.teamOne = teamOne;
this.teamTwo = teamTwo;
this.matchType = matchType;
this.dateTimeGMT = dateTimeGMT;
this.squad = squad;
this.tossWinnerTeam = tossWinnerTeam;
this.winnerTeam = winnerTeam;
this.matchStarted = matchStarted;
}
@Override
public String toString() {
return super.toString();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTeamOne() {
return teamOne;
}
public void setTeamOne(String teamOne) {
this.teamOne = teamOne;
}
public String getTeamTwo() {
return teamTwo;
}
public void setTeamTwo(String teamTwo) {
this.teamTwo = teamTwo;
}
public String getMatchType() {
return matchType;
}
public void setMatchType(String matchType) {
this.matchType = matchType;
}
public String getDateTimeGMT() {
return dateTimeGMT;
}
public void setDateTimeGMT(String dateTimeGMT) {
this.dateTimeGMT = dateTimeGMT;
}
public boolean isSquad() {
return squad;
}
public void setSquad(boolean squad) {
this.squad = squad;
}
public String getTossWinnerTeam() {
return tossWinnerTeam;
}
public void setTossWinnerTeam(String tossWinnerTeam) {
this.tossWinnerTeam = tossWinnerTeam;
}
public String getWinnerTeam() {
return winnerTeam;
}
public void setWinnerTeam(String winnerTeam) {
this.winnerTeam = winnerTeam;
}
public boolean isMatchStarted() {
return matchStarted;
}
public void setMatchStarted(boolean matchStarted) {
this.matchStarted = matchStarted;
}
}
}
我有主要活动
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<NewMatchList> apiList=apiService.getNewMatchListCallListCall(API_KEY);
apiList.enqueue(new Callback<NewMatchList>() {
@Override
public void onResponse(Call<NewMatchList> call, Response<NewMatchList> response) {
NewMatchList newMatchList=response.body();
Log.d("ApiCall",response.toString());
Log.d("ApiCall_22",newMatchList.toString());
Toast.makeText(getApplicationContext(),"inside",Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<NewMatchList> call, Throwable t) {
Log.d("ApiCall_Error",t.toString());
}
});
}
我有接口
public interface ApiInterface {
@GET("matches")
Call<NewMatchList> getNewMatchListCallListCall(@Query("apikey") String apiKey);
}
但每次在日志中我都会获得以下结果。
com.example.project.volleyretroprofit.Model.NewMatchList@4195b478
如果我尝试访问response.body().getMatchList()
那么空指针异常
任何人都可以帮助我,我错了
更新
public class ApiClient {
public static final String BASE_URL = "http://cricapi.com/api/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
更新了Match.java
public class Match {
@SerializedName("unique_id")
@Expose
private Integer uniqueId;
@SerializedName("date")
@Expose
private String date;
@SerializedName("team-2")
@Expose
private String team2;
@SerializedName("team-1")
@Expose
private String team1;
@SerializedName("type")
@Expose
private String type;
@SerializedName("dateTimeGMT")
@Expose
private String dateTimeGMT;
@SerializedName("squad")
@Expose
private Boolean squad;
@SerializedName("toss_winner_team")
@Expose
private String tossWinnerTeam;
@SerializedName("matchStarted")
@Expose
private Boolean matchStarted;
@SerializedName("winner_team")
@Expose
private String winnerTeam;
public Integer getUniqueId() {
return uniqueId;
}
public void setUniqueId(Integer uniqueId) {
this.uniqueId = uniqueId;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTeam2() {
return team2;
}
public void setTeam2(String team2) {
this.team2 = team2;
}
public String getTeam1() {
return team1;
}
public void setTeam1(String team1) {
this.team1 = team1;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDateTimeGMT() {
return dateTimeGMT;
}
public void setDateTimeGMT(String dateTimeGMT) {
this.dateTimeGMT = dateTimeGMT;
}
public Boolean getSquad() {
return squad;
}
public void setSquad(Boolean squad) {
this.squad = squad;
}
public String getTossWinnerTeam() {
return tossWinnerTeam;
}
public void setTossWinnerTeam(String tossWinnerTeam) {
this.tossWinnerTeam = tossWinnerTeam;
}
public Boolean getMatchStarted() {
return matchStarted;
}
public void setMatchStarted(Boolean matchStarted) {
this.matchStarted = matchStarted;
}
public String getWinnerTeam() {
return winnerTeam;
}
public void setWinnerTeam(String winnerTeam) {
this.winnerTeam = winnerTeam;
}
}
更新了NewMatchList.java
public class NewMatchList {
@SerializedName("matches")
@Expose
List<Match> matchList;
public NewMatchList(List<Match> matchList) {
this.matchList = matchList;
}
public List<Match> getMatchList() {
return matchList;
}
public void setMatchList(List<Match> matchList) {
this.matchList = matchList;
}
}
答案 0 :(得分:4)
您的密钥名称应与模型类中的成员变量名称匹配。
做这样的事情
public class NewMatchList {
@SerializedName("matches")
@Expose
List<MatchList> matchList;
public NewMatchList(List<MatchList> matchList) {
this.matchList = matchList;
}
public List<MatchList> getMatchList() {
return matchList;
}
public void setMatchList(List<MatchList> matchList) {
this.matchList = matchList;
}
public class MatchList{
@SerializedName("unique_id")
@Expose
private int id;
@SerializedName("date")
@Expose
private String date;
@SerializedName("team-1")
@Expose
private String teamOne;
@SerializedName("team-2")
@Expose
private String teamTwo;
@SerializedName("type")
@Expose
private String matchType;
@SerializedName("dateTimeGMT")
@Expose
private String dateTimeGMT;
@SerializedName("squad")
@Expose
private boolean squad;
@SerializedName("toss_winner_team")
@Expose
private String tossWinnerTeam;
@SerializedName("matchStarted")
@Expose
private String winnerTeam;
@SerializedName("winner_team")
@Expose
private boolean matchStarted;
public MatchList(int id, String date, String teamOne, String teamTwo, String matchType, String dateTimeGMT, boolean squad, String tossWinnerTeam, String winnerTeam, boolean matchStarted) {
this.id = id;
this.date = date;
this.teamOne = teamOne;
this.teamTwo = teamTwo;
this.matchType = matchType;
this.dateTimeGMT = dateTimeGMT;
this.squad = squad;
this.tossWinnerTeam = tossWinnerTeam;
this.winnerTeam = winnerTeam;
this.matchStarted = matchStarted;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTeamOne() {
return teamOne;
}
public void setTeamOne(String teamOne) {
this.teamOne = teamOne;
}
public String getTeamTwo() {
return teamTwo;
}
public void setTeamTwo(String teamTwo) {
this.teamTwo = teamTwo;
}
public String getMatchType() {
return matchType;
}
public void setMatchType(String matchType) {
this.matchType = matchType;
}
public String getDateTimeGMT() {
return dateTimeGMT;
}
public void setDateTimeGMT(String dateTimeGMT) {
this.dateTimeGMT = dateTimeGMT;
}
public boolean isSquad() {
return squad;
}
public void setSquad(boolean squad) {
this.squad = squad;
}
public String getTossWinnerTeam() {
return tossWinnerTeam;
}
public void setTossWinnerTeam(String tossWinnerTeam) {
this.tossWinnerTeam = tossWinnerTeam;
}
public String getWinnerTeam() {
return winnerTeam;
}
public void setWinnerTeam(String winnerTeam) {
this.winnerTeam = winnerTeam;
}
public boolean isMatchStarted() {
return matchStarted;
}
public void setMatchStarted(boolean matchStarted) {
this.matchStarted = matchStarted;
}
}
}