主要活动:
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:
{“post_path”: “/nn4cejmhjhbjbsp3vvvhtnwlgqg.jpg”,
您得到的是一个对象,但需要的是一个数组。您需要一个POJO模型来响应population
endpoint。根据文档,它看起来是这样的--
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);
}
更新适配器以提取结果
以用作MoviModel
的列表
如何获得原始json输出。如果可能的话,我不想实现用户数据类和解析器。有什么办法吗? 标记重复的帖子(获得原始HTTP响应与改造)不是为Kotlin和我需要Kotlin版本。
在referfit中,您只能读取response.body()一次,因为它是一个流,一旦调用或自动转换为返回类型中的任何模型类,它就会自动关闭。如果你尝试读两次,那么你会得到上面的错误。 我既需要原始响应字符串,也需要模型类。做这件事最好的方法是什么?我不想调用两次API。有什么方法可以复制响应体吗?理想情况下,我希望简单地获取String并返回响应。也就是说,不必放弃改型带来的通用类型转换器的好
如何将此Json响应转换为改装bean我得到了Gson错误,就像使用JsonReader一样。setLenient(true)在第1行第1列路径接受格式错误的JSON$ api调用为 字符串CATEGORY\u API=“”https://api.callingservice.com"; 请帮助我解决这个问题,如何将Json响应转换为Bean,我的Bean类如下
问题内容: 我正在尝试使用以下代码将值从servlet传递到jsp页面: 当我运行页面时,我得到: 错误: 我尝试使用: 但是我遇到了同样的错误。 如何解决这个错误? 问题答案: 您已经在catch代码块中转发了响应: 因此,您不能再次调用: 因为它已经转发(提交)。 因此,您可以做的是:保留一个字符串以分配需要转发响应的位置。
问题内容: 如何确定Jquery中Ajax调用的响应类型?有时,服务器发送json响应,有时仅发送html进行显示。现在我正在使用 问题答案: 您可以尝试如下操作: 基本上也使用indexOf,但似乎更可靠。