Search code examples
androidretrofitpicasso

How to Debug Why No Response with Retrofit


I am trying to get a response using Retrofit and display images from a url within the json response. I don't know how to debug for Retrofit. I get into the "failure" block and see my "Fail" toast. Here is some code:

Main Activity:

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();
            }
        });
    }
}

My Adapter:

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;
    }
}

And my interface:

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;
}

}

Sample JSON:

{ "page": 1,

"results": [

{ "poster_path": "/D6e8RJf2qUstnfkTslTXNTUAlT.jpg", "adult": false, "overview": "Armed with the astonishing ability to shrink in scale but increase in strength, con-man Scott Lang must embrace his inner-hero and help his mentor, Dr. Hank Pym, protect the secret behind his spectacular Ant-Man suit from a new generation of towering threats. Against seemingly insurmountable obstacles, Pym and Lang must plan and pull off a heist that will save the world.", "release_date": "2015-07-17",

"genre_ids": [ 878, 28, 12 ], "id": 102899, "original_title": "Ant-Man", "original_language": "en", "title": "Ant-Man", "backdrop_path": "/kvXLZqY0Ngl1XSw7EaMQO0C1CCj.jpg", "popularity": 52.456352, "vote_count": 1931, "video": false, "vote_average": 6.9 },

{ "poster_path": "/nN4cEJMHJHbJBsp3vvvhtNWLGqg.jpg",


Solution

  • You are getting an object, but expecting an array. You need a POJO model for the response of the popular endpoint. Based on the docs, it looks something like this --

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

    then change your interface to --

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

    Update your adapter to extract results to use as your list of MovieModels