无法从Retrofit 2得到回应

时间:2017-11-24 08:24:21

标签: android json retrofit2

使用Retrofit无法理解错误在哪里。只是从入队中返回任何东西。即我的结果和列表是0;给出了Json响应

Retrofit.Builder builder = new Retrofit.Builder()
    .baseUrl("https://api.themoviedb.org/3/")
    .addConverterFactory(GsonConverterFactory.create());

Retrofit retrofit = builder.build();
MovieClient client = retrofit.create(MovieClient.class);

Call<Result> resultCall = client.getMoviesList();

List<Movie> resultFromApi = new ArrayList<>();

resultCall.enqueue(new Callback<Result>() {
  @Override
  public void onResponse(Call<Result> call, Response<Result> response) {
    resultFromApi.addAll(response.body().getResults());
  }

  @Override
  public void onFailure(Call<Result> call, Throwable t) {
    //callback.onDataNotAvailable();
    Log.e(TAG, "onFailure: error");
  }
});

MovieClient.java

@GET("movie/popular?api_key=my_key")
Call<Result>getMoviesList();

Result.java

@SerializedName("results")
@Expose
private List<Movie> results;

public List<Movie> getResults() {
    if (results == null) {
        return new ArrayList<>();
    }
    return results;
}

来自API提供的JSON响应

JSON回复:

enter image description here

提前谢谢!

1 个答案:

答案 0 :(得分:0)

你的Movie.java类必须是这样的

public class Movie {

@SerializedName("vote_count")
@Expose
private Integer voteCount;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("video")
@Expose
private Boolean video;
@SerializedName("vote_average")
@Expose
private Double voteAverage;
@SerializedName("title")
@Expose
private String title;
@SerializedName("popularity")
@Expose
private Double popularity;
@SerializedName("poster_path")
@Expose
private String posterPath;
@SerializedName("original_language")
@Expose
private String originalLanguage;
@SerializedName("original_title")
@Expose
private String originalTitle;
@SerializedName("genre_ids")
@Expose
private List<Integer> genreIds = null;
@SerializedName("backdrop_path")
@Expose
private String backdropPath;
@SerializedName("adult")
@Expose
private Boolean adult;
@SerializedName("overview")
@Expose
private String overview;
@SerializedName("release_date")
@Expose
private String releaseDate;

public Integer getVoteCount() {
    return voteCount;
}

public void setVoteCount(Integer voteCount) {
    this.voteCount = voteCount;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public Boolean getVideo() {
    return video;
}

public void setVideo(Boolean video) {
    this.video = video;
}

public Double getVoteAverage() {
    return voteAverage;
}

public void setVoteAverage(Double voteAverage) {
    this.voteAverage = voteAverage;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public Double getPopularity() {
    return popularity;
}

public void setPopularity(Double popularity) {
    this.popularity = popularity;
}

public String getPosterPath() {
    return posterPath;
}

public void setPosterPath(String posterPath) {
    this.posterPath = posterPath;
}

public String getOriginalLanguage() {
    return originalLanguage;
}

public void setOriginalLanguage(String originalLanguage) {
    this.originalLanguage = originalLanguage;
}

public String getOriginalTitle() {
    return originalTitle;
}

public void setOriginalTitle(String originalTitle) {
    this.originalTitle = originalTitle;
}

public List<Integer> getGenreIds() {
    return genreIds;
}

public void setGenreIds(List<Integer> genreIds) {
    this.genreIds = genreIds;
}

public String getBackdropPath() {
    return backdropPath;
}

public void setBackdropPath(String backdropPath) {
    this.backdropPath = backdropPath;
}

public Boolean getAdult() {
    return adult;
}

public void setAdult(Boolean adult) {
    this.adult = adult;
}

public String getOverview() {
    return overview;
}

public void setOverview(String overview) {
    this.overview = overview;
}

public String getReleaseDate() {
    return releaseDate;
}

public void setReleaseDate(String releaseDate) {
    this.releaseDate = releaseDate;
}

}