如何调试为什么没有响应与改造

时间:2015-12-15 03:15:35

标签: android retrofit picasso

我正在尝试使用Retrofit获取响应并在json响应中显示来自url的图像。我不知道如何调试Retrofit。我进入“失败”区块并看到我的“失败”吐司。这是一些代码:

主要活动:

public class TheMovieDbActivity extends Activity {

    private GridView gridView;
    private List<MovieModel> movieModelsList;
    private String ENDPOINT_URL = "https://api.themoviedb.org";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.movie_db_activity);

        gridView = (GridView) findViewById(R.id.gridView);

        final RestAdapter restadapter = new RestAdapter.Builder().setEndpoint(ENDPOINT_URL).build();

        apiLocation apiLocation = restadapter.create(apiLocation.class);

        apiLocation.getData(new Callback<List<MovieModel>>() {
            @Override
            public void success(List<MovieModel> movieModels, Response response) {
                movieModelsList = movieModels;
                MoviesGridViewAdapter adapter = new MoviesGridViewAdapter(getApplicationContext(), R.layout.movie_gridview_item, movieModelsList);
                gridView.setAdapter(adapter);
            }


            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

我的适配器:

public class MoviesGridViewAdapter extends ArrayAdapter<MovieModel> {

    String url="http://services.hanselandpetal.com/photos/";

    private Context context;
    private List<MovieModel> movieModelList;
    LayoutInflater inflater;

    public MoviesGridViewAdapter(Context context, int resource, List<MovieModel> objects) {
        super(context,resource,objects);
        this.context = context;
        this.movieModelList = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.movie_gridview_item, parent, false);
        MovieModel movieModel = movieModelList.get(position);
        ImageView img = (ImageView) view.findViewById(R.id.grid_item_image);
        Picasso.with(getContext()).load(url+movieModel.getPosterPath()).resize(100, 100).into(img);
        return view;
    }
}

我的界面:

public interface apiLocation {
    @GET("/3/movie/popular?my api key")
    void getData(Callback<List<MovieModel>> response);
}

POJO:

public class MovieModel {


@SerializedName("adult")
@Expose
private Boolean adult;
@SerializedName("backdrop_path")
@Expose
private String backdropPath;
@SerializedName("belongs_to_collection")
@Expose
private Object belongsToCollection;
@SerializedName("budget")
@Expose
private Integer budget;
@SerializedName("genres")
@Expose
private List<Genre> genres = new ArrayList<Genre>();
@SerializedName("homepage")
@Expose
private String homepage;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("imdb_id")
@Expose
private String imdbId;
@SerializedName("original_language")
@Expose
private String originalLanguage;
@SerializedName("original_title")
@Expose
private String originalTitle;
@SerializedName("overview")
@Expose
private String overview;
@SerializedName("popularity")
@Expose
private Double popularity;
@SerializedName("poster_path")
@Expose
private String posterPath;
@SerializedName("production_companies")
@Expose
private List<ProductionCompany> productionCompanies = new ArrayList<ProductionCompany>();
@SerializedName("production_countries")
@Expose
private List<ProductionCountry> productionCountries = new ArrayList<ProductionCountry>();
@SerializedName("release_date")
@Expose
private String releaseDate;
@SerializedName("revenue")
@Expose
private Integer revenue;
@SerializedName("runtime")
@Expose
private Integer runtime;
@SerializedName("spoken_languages")
@Expose
private List<SpokenLanguage> spokenLanguages = new ArrayList<SpokenLanguage>();
@SerializedName("status")
@Expose
private String status;
@SerializedName("tagline")
@Expose
private String tagline;
@SerializedName("title")
@Expose
private String title;
@SerializedName("video")
@Expose
private Boolean video;
@SerializedName("vote_average")
@Expose
private Double voteAverage;
@SerializedName("vote_count")
@Expose
private Integer voteCount;

/**
 *
 * @return
 *     The adult
 */
public Boolean getAdult() {
    return adult;
}

/**
 *
 * @param adult
 *     The adult
 */
public void setAdult(Boolean adult) {
    this.adult = adult;
}

/**
 *
 * @return
 *     The backdropPath
 */
public String getBackdropPath() {
    return backdropPath;
}

/**
 *
 * @param backdropPath
 *     The backdrop_path
 */
public void setBackdropPath(String backdropPath) {
    this.backdropPath = backdropPath;
}

/**
 *
 * @return
 *     The belongsToCollection
 */
public Object getBelongsToCollection() {
    return belongsToCollection;
}

/**
 *
 * @param belongsToCollection
 *     The belongs_to_collection
 */
public void setBelongsToCollection(Object belongsToCollection) {
    this.belongsToCollection = belongsToCollection;
}

/**
 *
 * @return
 *     The budget
 */
public Integer getBudget() {
    return budget;
}

/**
 *
 * @param budget
 *     The budget
 */
public void setBudget(Integer budget) {
    this.budget = budget;
}

/**
 *
 * @return
 *     The genres
 */
public List<Genre> getGenres() {
    return genres;
}

/**
 *
 * @param genres
 *     The genres
 */
public void setGenres(List<Genre> genres) {
    this.genres = genres;
}

/**
 *
 * @return
 *     The homepage
 */
public String getHomepage() {
    return homepage;
}

/**
 *
 * @param homepage
 *     The homepage
 */
public void setHomepage(String homepage) {
    this.homepage = homepage;
}

/**
 *
 * @return
 *     The id
 */
public Integer getId() {
    return id;
}

/**
 *
 * @param id
 *     The id
 */
public void setId(Integer id) {
    this.id = id;
}

/**
 *
 * @return
 *     The imdbId
 */
public String getImdbId() {
    return imdbId;
}

/**
 *
 * @param imdbId
 *     The imdb_id
 */
public void setImdbId(String imdbId) {
    this.imdbId = imdbId;
}

/**
 *
 * @return
 *     The originalLanguage
 */
public String getOriginalLanguage() {
    return originalLanguage;
}

/**
 *
 * @param originalLanguage
 *     The original_language
 */
public void setOriginalLanguage(String originalLanguage) {
    this.originalLanguage = originalLanguage;
}

/**
 *
 * @return
 *     The originalTitle
 */
public String getOriginalTitle() {
    return originalTitle;
}

/**
 *
 * @param originalTitle
 *     The original_title
 */
public void setOriginalTitle(String originalTitle) {
    this.originalTitle = originalTitle;
}

/**
 *
 * @return
 *     The overview
 */
public String getOverview() {
    return overview;
}

/**
 *
 * @param overview
 *     The overview
 */
public void setOverview(String overview) {
    this.overview = overview;
}

/**
 *
 * @return
 *     The popularity
 */
public Double getPopularity() {
    return popularity;
}

/**
 *
 * @param popularity
 *     The popularity
 */
public void setPopularity(Double popularity) {
    this.popularity = popularity;
}

/**
 *
 * @return
 *     The posterPath
 */
public String getPosterPath() {
    return posterPath;
}

/**
 *
 * @param posterPath
 *     The poster_path
 */
public void setPosterPath(String posterPath) {
    this.posterPath = posterPath;
}

/**
 *
 * @return
 *     The productionCompanies
 */
public List<ProductionCompany> getProductionCompanies() {
    return productionCompanies;
}

/**
 *
 * @param productionCompanies
 *     The production_companies
 */
public void setProductionCompanies(List<ProductionCompany> productionCompanies) {
    this.productionCompanies = productionCompanies;
}

/**
 *
 * @return
 *     The productionCountries
 */
public List<ProductionCountry> getProductionCountries() {
    return productionCountries;
}

/**
 *
 * @param productionCountries
 *     The production_countries
 */
public void setProductionCountries(List<ProductionCountry> productionCountries) {
    this.productionCountries = productionCountries;
}

/**
 *
 * @return
 *     The releaseDate
 */
public String getReleaseDate() {
    return releaseDate;
}

/**
 *
 * @param releaseDate
 *     The release_date
 */
public void setReleaseDate(String releaseDate) {
    this.releaseDate = releaseDate;
}

/**
 *
 * @return
 *     The revenue
 */
public Integer getRevenue() {
    return revenue;
}

/**
 *
 * @param revenue
 *     The revenue
 */
public void setRevenue(Integer revenue) {
    this.revenue = revenue;
}

/**
 *
 * @return
 *     The runtime
 */
public Integer getRuntime() {
    return runtime;
}

/**
 *
 * @param runtime
 *     The runtime
 */
public void setRuntime(Integer runtime) {
    this.runtime = runtime;
}

/**
 *
 * @return
 *     The spokenLanguages
 */
public List<SpokenLanguage> getSpokenLanguages() {
    return spokenLanguages;
}

/**
 *
 * @param spokenLanguages
 *     The spoken_languages
 */
public void setSpokenLanguages(List<SpokenLanguage> spokenLanguages) {
    this.spokenLanguages = spokenLanguages;
}

/**
 *
 * @return
 *     The status
 */
public String getStatus() {
    return status;
}

/**
 *
 * @param status
 *     The status
 */
public void setStatus(String status) {
    this.status = status;
}

/**
 *
 * @return
 *     The tagline
 */
public String getTagline() {
    return tagline;
}

/**
 *
 * @param tagline
 *     The tagline
 */
public void setTagline(String tagline) {
    this.tagline = tagline;
}

/**
 *
 * @return
 *     The title
 */
public String getTitle() {
    return title;
}

/**
 *
 * @param title
 *     The title
 */
public void setTitle(String title) {
    this.title = title;
}

/**
 *
 * @return
 *     The video
 */
public Boolean getVideo() {
    return video;
}

/**
 *
 * @param video
 *     The video
 */
public void setVideo(Boolean video) {
    this.video = video;
}

/**
 *
 * @return
 *     The voteAverage
 */
public Double getVoteAverage() {
    return voteAverage;
}

/**
 *
 * @param voteAverage
 *     The vote_average
 */
public void setVoteAverage(Double voteAverage) {
    this.voteAverage = voteAverage;
}

/**
 *
 * @return
 *     The voteCount
 */
public Integer getVoteCount() {
    return voteCount;
}

/**
 *
 * @param voteCount
 *     The vote_count
 */
public void setVoteCount(Integer voteCount) {
    this.voteCount = voteCount;
}

}

示例JSON:

  

{   “页面”:1,

“结果”:[

{ “poster_path”: “/D6e8RJf2qUstnfkTslTXNTUAlT.jpg” “成人”:假的, “概述”:“凭借惊人的规模缩小但力量增强的能力,骗子Scott Lang必须拥抱他的内心英雄并帮助他的导师Hank Pym博士保护他壮观的Ant-Man套装背后的秘密来自新一代高耸的威胁。对于看似无法克服的障碍,Pym和Lang必须计划并推出一个能够拯救世界的抢劫。“, “RELEASE_DATE”: “2015年7月17日”,

“genre_ids”:[ 878, 28, 12 ] “ID”:102899, “ORIGINAL_TITLE”:“蚁人”, “ORIGINAL_LANGUAGE”: “恩”, “头衔”:“蚁人”, “backdrop_path”: “/kvXLZqY0Ngl1XSw7EaMQO0C1CCj.jpg” “人气”:52.456352, “vote_count”:1931年, “视频”:假的, “vote_average”:6.9 },

{ “poster_path”: “/nN4cEJMHJHbJBsp3vvvhtNWLGqg.jpg”,

1 个答案:

答案 0 :(得分:0)

您正在获取一个对象,但期待一个数组。您需要一个POJO模型来响应popular端点的响应。根据文档,它看起来像这样 -

class MovieDBResponse {
    int page;
    List<MovieModel> results;
    int total_pages;
    int total_results;
}

然后将您的界面更改为 -

public interface apiLocation {
    @GET("/3/movie/popular?my api key")
    void getData(Callback<List<MovieDBResponse>> response);
}

更新适配器以解压缩results以用作MovieModel s

的列表