Recyclerview 中不显示 Retrofit 响应对象
Retrofit response object is not displayed in recyclerview
我从网络上获取了一些 json 数据,我正在使用改造在我的存储库中检索这些数据并将其发送到我的视图模型以显示在 activity 中。响应全部成功,我验证了我的 POJO 匹配 json。当我创建硬编码的团队对象并将其从存储库响应发送到我的视图模型时,我得到的数据显示在 UI 中,但不是来自响应。这是我的存储库。
private TeamRepo() {
teamMutableLiveData = new MutableLiveData<>();
}
public MutableLiveData<Team> getTeam(final String team){
RetrofitServiceApi retrofitServiceApi = RetrofitService.getRetrofitInstance()
.create(RetrofitServiceApi.class);
Call<Team> teamCall = retrofitServiceApi.getTeam(team);
teamCall.enqueue(new Callback<Team>() {
@Override
public void onResponse(Call<Team> call, Response<Team> response) {
if(response.isSuccessful()) {
Log.e("response", response.toString());
teamMutableLiveData.setValue(response.body());
if (response.body() != null) {
Log.e("response body", response.message());
}
//teamMutableLiveData.setValue(team);
}
}
@Override
public void onFailure(Call<Team> call, Throwable t) {
Log.e("error response", t.toString());
//TODO
}
});
return teamMutableLiveData;
}
我的 api 如下所示。
@GET("api/v1/json/1/searchteams.php")
Call<Team> getTeam(@Query("t") String team);
服务是
private static Retrofit retrofit;
private static final String BASE_URL = "https://www.thesportsdb.com/";
public static synchronized Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
https://www.thesportsdb.com/api/v1/json/1/searchteams.php?t=Arsenal
部分json数据如下
{
"teams": [
{
"idTeam": "133604",
"idSoccerXML": "9",
"idAPIfootball": "42",
"intLoved": "2",
"strTeam": "Arsenal",
"strTeamShort": "Ars",
"strAlternate": "Gunners, Arsenal Football Club, AFC",
"intFormedYear": "1892",
}
]
}
POJO战队如下
public class Team {
@SerializedName("idTeam")
private String idTeam;
@SerializedName("idSoccerXML")
private String idSoccerXML;
@SerializedName("idAPIfootball")
private String idAPIfootball;
@SerializedName("intLoved")
private String intLoved;
@SerializedName("strTeam")
private String strTeam;
@SerializedName("strTeamShort")
private String strTeamShort;
@SerializedName("strAlternate")
private String strAlternate;
@SerializedName("intFormedYear")
private String intFormedYear;
public String getIdTeam() {
return idTeam;
}
public void setIdTeam(String idTeam) {
this.idTeam = idTeam;
}
public String getIdSoccerXML() {
return idSoccerXML;
}
public void setIdSoccerXML(String idSoccerXML) {
this.idSoccerXML = idSoccerXML;
}
public String getIdAPIfootball() {
return idAPIfootball;
}
public void setIdAPIfootball(String idAPIfootball) {
this.idAPIfootball = idAPIfootball;
}
public String getIntLoved() {
return intLoved;
}
public void setIntLoved(String intLoved) {
this.intLoved = intLoved;
}
public String getStrTeam() {
return strTeam;
}
public void setStrTeam(String strTeam) {
this.strTeam = strTeam;
}
public String getStrTeamShort() {
return strTeamShort;
}
public void setStrTeamShort(String strTeamShort) {
this.strTeamShort = strTeamShort;
}
public String getStrAlternate() {
return strAlternate;
}
public void setStrAlternate(String strAlternate) {
this.strAlternate = strAlternate;
}
public String getIntFormedYear() {
return intFormedYear;
}
public void setIntFormedYear(String intFormedYear) {
this.intFormedYear = intFormedYear;
}
}
服务器响应是一个 JSON 包含“teams”数组的对象。您需要创建一个 SearchResult
对象:
public class SearchResult {
@SerializedName("teams")
private List<Team> teams;
public List<Team> getTeams() {
return teams;
}
}
然后将您对 Retrofit 的调用替换为 return 一个 SearchResult
对象:
@GET("api/v1/json/1/searchteams.php")
Call<SearchResult> getTeam(@Query("t") String team);
您将在
中得到结果
response.body().getTeams(); // Don't forget to check for nulls
我从网络上获取了一些 json 数据,我正在使用改造在我的存储库中检索这些数据并将其发送到我的视图模型以显示在 activity 中。响应全部成功,我验证了我的 POJO 匹配 json。当我创建硬编码的团队对象并将其从存储库响应发送到我的视图模型时,我得到的数据显示在 UI 中,但不是来自响应。这是我的存储库。
private TeamRepo() {
teamMutableLiveData = new MutableLiveData<>();
}
public MutableLiveData<Team> getTeam(final String team){
RetrofitServiceApi retrofitServiceApi = RetrofitService.getRetrofitInstance()
.create(RetrofitServiceApi.class);
Call<Team> teamCall = retrofitServiceApi.getTeam(team);
teamCall.enqueue(new Callback<Team>() {
@Override
public void onResponse(Call<Team> call, Response<Team> response) {
if(response.isSuccessful()) {
Log.e("response", response.toString());
teamMutableLiveData.setValue(response.body());
if (response.body() != null) {
Log.e("response body", response.message());
}
//teamMutableLiveData.setValue(team);
}
}
@Override
public void onFailure(Call<Team> call, Throwable t) {
Log.e("error response", t.toString());
//TODO
}
});
return teamMutableLiveData;
}
我的 api 如下所示。
@GET("api/v1/json/1/searchteams.php")
Call<Team> getTeam(@Query("t") String team);
服务是
private static Retrofit retrofit;
private static final String BASE_URL = "https://www.thesportsdb.com/";
public static synchronized Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
https://www.thesportsdb.com/api/v1/json/1/searchteams.php?t=Arsenal
部分json数据如下
{
"teams": [
{
"idTeam": "133604",
"idSoccerXML": "9",
"idAPIfootball": "42",
"intLoved": "2",
"strTeam": "Arsenal",
"strTeamShort": "Ars",
"strAlternate": "Gunners, Arsenal Football Club, AFC",
"intFormedYear": "1892",
}
]
}
POJO战队如下
public class Team {
@SerializedName("idTeam")
private String idTeam;
@SerializedName("idSoccerXML")
private String idSoccerXML;
@SerializedName("idAPIfootball")
private String idAPIfootball;
@SerializedName("intLoved")
private String intLoved;
@SerializedName("strTeam")
private String strTeam;
@SerializedName("strTeamShort")
private String strTeamShort;
@SerializedName("strAlternate")
private String strAlternate;
@SerializedName("intFormedYear")
private String intFormedYear;
public String getIdTeam() {
return idTeam;
}
public void setIdTeam(String idTeam) {
this.idTeam = idTeam;
}
public String getIdSoccerXML() {
return idSoccerXML;
}
public void setIdSoccerXML(String idSoccerXML) {
this.idSoccerXML = idSoccerXML;
}
public String getIdAPIfootball() {
return idAPIfootball;
}
public void setIdAPIfootball(String idAPIfootball) {
this.idAPIfootball = idAPIfootball;
}
public String getIntLoved() {
return intLoved;
}
public void setIntLoved(String intLoved) {
this.intLoved = intLoved;
}
public String getStrTeam() {
return strTeam;
}
public void setStrTeam(String strTeam) {
this.strTeam = strTeam;
}
public String getStrTeamShort() {
return strTeamShort;
}
public void setStrTeamShort(String strTeamShort) {
this.strTeamShort = strTeamShort;
}
public String getStrAlternate() {
return strAlternate;
}
public void setStrAlternate(String strAlternate) {
this.strAlternate = strAlternate;
}
public String getIntFormedYear() {
return intFormedYear;
}
public void setIntFormedYear(String intFormedYear) {
this.intFormedYear = intFormedYear;
}
}
服务器响应是一个 JSON 包含“teams”数组的对象。您需要创建一个 SearchResult
对象:
public class SearchResult {
@SerializedName("teams")
private List<Team> teams;
public List<Team> getTeams() {
return teams;
}
}
然后将您对 Retrofit 的调用替换为 return 一个 SearchResult
对象:
@GET("api/v1/json/1/searchteams.php")
Call<SearchResult> getTeam(@Query("t") String team);
您将在
中得到结果response.body().getTeams(); // Don't forget to check for nulls