2015-12-15 3 views
0

Я пытаюсь получить ответ с помощью «Дооснащения» и отображения изображений с URL-адреса в ответном сообщении json. Я не знаю, как отлаживать для Retrofit. Я попадаю в блок «Сбой» и вижу тосты «Fail». Вот код:Как отлаживать Почему нет ответа с помощью дооснащения

Основная деятельность:

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:

{ "page": 1,

"Результаты": [

{ "poster_path": "/D6e8RJf2qUstnfkTslTXNTUAlT.jpg", "для взрослых": ложь, "Обзор" : «Вооруженный удивительной способностью уменьшаться в масштабе, но увеличивая силу, человек Скотт Лэнг должен обнять своего внутреннего героя и помочь своему наставнику, доктору Хэнку Пиму, защитить секрет своего впечатляющего костюма Ант-Ман от нового генерации возвышающихся угроз. На первый взгляд непреодолимые препятствия, Пим и Lang должны планировать и снять ограбление, которая спасет мир ", "RELEASE_DATE.": "2015-07-17",

"genre_ids": [ 878, 28 , ], "идентификатор": 102899, "ORIGINAL_TITLE": "Человек-муравей", "original_language": "ан", "название": "Человек-муравей", "backdrop_path": " /kvXLZqY0Ngl1XSw7EaMQO0C1CCj.jpg», "популярность": 52,456352, "vote_count": 1931, "видео": ложь, "vote_average": 6,9 },

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

+0

почему вы не пытаетесь зарегистрировать эту ошибку? – Sree

+0

duh! Благодаря! Наверное, я действительно слишком устал. Моя первая ошибка была несанкционированной из-за забывания параметра в моем URL-адресе. Теперь у меня есть эта ошибка .... retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: ожидается BEGIN_ARRAY, но BEGIN_OBJECT в строке 1 столбец 2 путь $ – taraloca

+0

http://stackoverflow.com/a/ 24155082 – Sree

ответ

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

+0

Спасибо! Я делал что-то в этом роде, но имел много tweeking, чтобы получить url/base url и такое право. – taraloca

Смежные вопросы