Java 改造提供空Json响应的库
我正在使用改装库解析现在播放电影类别的TMDB API的Json响应,并希望在RecyclerView中显示结果 我从API获得的原始Json是Java 改造提供空Json响应的库,java,android,json,nullpointerexception,retrofit2,Java,Android,Json,Nullpointerexception,Retrofit2,我正在使用改装库解析现在播放电影类别的TMDB API的Json响应,并希望在RecyclerView中显示结果 我从API获得的原始Json是 { "page": 1, "results": [ { "poster_path": "\/tWqifoYuwLETmmasnGHO7xBjEtt.jpg", "adult": false, "overview": "A live-action adaptation of Disney's versi
{
"page": 1,
"results": [
{
"poster_path": "\/tWqifoYuwLETmmasnGHO7xBjEtt.jpg",
"adult": false,
"overview": "A live-action adaptation of Disney's version of the classic 'Beauty and the Beast' tale of a cursed prince and a beautiful young woman who helps him break the spell.",
"release_date": "2017-03-16",
"genre_ids": [
14,
10749
],
"id": 321612,
"original_title": "Beauty and the Beast",
"original_language": "en",
"title": "Beauty and the Beast",
"backdrop_path": "\/6aUWe0GSl69wMTSWWexsorMIvwU.jpg",
"popularity": 147.254531,
"vote_count": 1938,
"video": false,
"vote_average": 6.9
},
{
"poster_path": "\/unPB1iyEeTBcKiLg8W083rlViFH.jpg",
"adult": false,
"overview": "A story about how a new baby's arrival impacts a family, told from the point of view of a delightfully unreliable narrator, a wildly imaginative 7 year old named Tim.",
"release_date": "2017-03-23",
"genre_ids": [
16,
35,
10751
],
"id": 295693,
"original_title": "The Boss Baby",
"original_language": "en",
"title": "The Boss Baby",
"backdrop_path": "\/bTFeSwh07oX99ofpDI4O2WkiFJ.jpg",
"popularity": 138.856227,
"vote_count": 649,
"video": false,
"vote_average": 5.7
},
我创建的模型类是Movie.java
package com.example.vikas.movie.model;
/**
* Created by vikas on 22/4/17
*/
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
public class Movie
{
@SerializedName("poster_path")
private String posterPath;
@SerializedName("adult")
private boolean adult;
@SerializedName("overview")
private String overview;
@SerializedName("realease_date")
private String releaseDate;
@SerializedName("genre_ids")
private List<Integer> genreIds=new ArrayList<Integer>();
@SerializedName("id")
private int id;
@SerializedName("original_title")
private String originalTitle;
@SerializedName("original_language")
private String original_language;
@SerializedName("title")
private String title;
@SerializedName("backdrop_path")
private String backdrop_path;
@SerializedName("popularity")
private Double popularity;
@SerializedName("vote_count")
private int vote_count;
@SerializedName("vedeo")
private boolean vedeo;
@SerializedName("vote_average")
private Double vote_average;
public Movie(String posterPath, boolean adult, String overview, String releaseDate, List<Integer> genreIds, Integer id,
String originalTitle, String original_language, String title, String backdrop_path, Double popularity,
Integer vote_count, Boolean vedeo, Double vote_average) {
this.posterPath = posterPath;
this.adult = adult;
this.overview = overview;
this.releaseDate = releaseDate;
this.genreIds = genreIds;
this.id = id;
this.originalTitle = originalTitle;
this.original_language = original_language;
this.title = title;
this.backdrop_path = backdrop_path;
this.popularity = popularity;
this.vote_count = vote_count;
this.vedeo = vedeo;
this.vote_average = vote_average;
}
public String getPosterPath() {
return posterPath;
}
public void setPosterPath(String posterPath) {
this.posterPath = posterPath;
}
public boolean isAdult() {
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;
}
public List<Integer> getGenreIds() {
return genreIds;
}
public void setGenreIds(List<Integer> genreIds) {
this.genreIds = genreIds;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getOriginalTitle() {
return originalTitle;
}
public void setOriginalTitle(String originalTitle) {
this.originalTitle = originalTitle;
}
public String getOriginalLanguage() {
return original_language;
}
public void setOriginalLanguage(String original_language) {
this.original_language = original_language;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBackdropPath() {
return backdrop_path;
}
public void setBackdropPath(String backdropPath) {
this.backdrop_path = backdropPath;
}
public Double getPopularity() {
return popularity;
}
public void setPopularity(Double popularity) {
this.popularity = popularity;
}
public Integer getvote_count() {
return vote_count;
}
public void setVoteCount(Integer voteCount) {
this.vote_count = voteCount;
}
public Boolean getVedeo() {
return vedeo;
}
public void setVedeo(Boolean vedeo) {
this.vedeo = vedeo;
}
public Double getvote_average() {
return vote_average;
}
public void setVoteAverage(Double vote_average) {
this.vote_average = vote_average;
}
}
ApiInterface.java
package com.example.vikas.movie.rest;
/**
* Created by vikas on 23/4/17.
*/
import com.example.vikas.movie.model.MoviesResponse;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;
// to define endpoints of a URL we will use special retrofit annotations. we have used Get annotation in
// to get information from the API and the parameters os this method are @Query,@Path,
public interface ApiInterface
{
@GET("movie/now-playing")
Call<MoviesResponse> getNowPlayingMovies(@Query("api_key") String apikey);
//return value is alwayays a parameterized call<T> oject whose paremeter is generic so we can have
@GET("movie/{id}")
Call<MoviesResponse> getMovieDetails(@Path("id") int id, @Query("api_key") String apiKey);
}
// so using this we will generate the following URL
//http://api.themoviedb.org/3/movie/now-playing?api_key=Xxxx
那么我现在应该做什么来运行这段代码并获取结果以成功地显示结果,这样它就不会返回null呢
这个问题是不同的,因为首先我不能给出正确的URL,你的URL是错误的。应该是
http://api.themoviedb.org/3/movie/now_playing?api_key=ddfffa28b4ace50cacf67274370469a1
请注意结束点电影/正在播放而不是电影/正在播放
这记录在
您还可以在获得响应并初始化列表后获取列表大小
首先这个
movieList= response.body().getResults();
然后
编辑1:
你有这个
recyclerView.setAdapter(new MovieAdapter(movieList, R.layout.recyclerview_item_movie, getApplicationContext()));
您从不初始化movieAdapter
,当您调用notifyDataSetChanged()
时,movieAdapter
为空
换成
movieAdapter = new MovieAdapter(movieList, R.layout.recyclerview_item_movie, getApplicationContext());
然后
注意:get getApplicationContext()在大多数情况下与ui相关,可能没有用处。更喜欢活动上下文
编辑2:您还需要将项目添加到同一列表中,然后调用下面注释中建议的notifyDataSetChanged()
。替换
recyclerView.setAdapter(new MovieAdapter(movieList, R.layout.recyclerview_item_movie, getApplicationContext()));
与
替换
movieList= response.body().getResults();
movieAdapter.notifyDataSetChanged();
用这个。否则notifyDataSetChanged不会执行任何操作,因为您有一个完全不同的列表,需要重新创建适配器
movieList.clear();
movieList.addAll(response.body().getResults());
movieAdapter.notifyDataSetChanged();
等一段时间,我正在尝试你的答案你可以复制粘贴在浏览器的网址,以检查它是否工作。不需要在android上运行它…我已经尝试了你的答案,现在它显示的是电影大小20,但得到这个logcat错误和应用程序崩溃用新的logcat更新你的帖子。它与改装无关,因为您可以看到列表大小。这是一个不同的问题,另一个致命的例外:主进程:com.example.vikas.movie,PID:27956 java.lang.NullPointerException:尝试在null对象引用上调用虚拟方法'void com.example.vikas.movie.adapter.MovieAdapter.notifyDataSetChanged()'修复NullPointerException的方法始终相同。。。初始化stacktrace指向的对象,你能用更多的信息解释一下我们将如何得到一个完全不同的列表这就是
=
在Java中的工作方式吗?您正在为变量分配一个新的列表对象引用。谢谢,现在我明白了
Log.d(TAG, "Number of movies received: " + movieList.size());
recyclerView.setAdapter(new MovieAdapter(movieList, R.layout.recyclerview_item_movie, getApplicationContext()));
movieAdapter = new MovieAdapter(movieList, R.layout.recyclerview_item_movie, getApplicationContext());
recyclerView.setAdapter(movieAdapter);
recyclerView.setAdapter(new MovieAdapter(movieList, R.layout.recyclerview_item_movie, getApplicationContext()));
movieAdapter = new MovieAdapter(movieList, R.layout.recyclerview_item_movie, this);
recyclerView.setAdapter(movieAdapter);
movieList= response.body().getResults();
movieAdapter.notifyDataSetChanged();
movieList.clear();
movieList.addAll(response.body().getResults());
movieAdapter.notifyDataSetChanged();