如何正确使用Retrofit解析JSON并在RecyclerView中实现它?

时间:2019-05-07 08:18:58

标签: mvvm gson repository retrofit

我正在尝试构建一个具有2个标签的应用,这些标签使用themoviedb.org API显示电影和电视节目。它应该使用MVVM模型,我真的感到困惑,因为距我开始使用Android编码仅两个月了。如何正确地将其传递给每个类,并在存储库类中正确使用Retrofit方法?

这个想法是通过Repository类访问API并将数据传递给ViewModel类,然后使用RecyclerView在MovieFragment中显示它,但看来我无法通过每个类传递数据。

+ MovieRepository类

    public class MovieRepository {
     private static MovieRepository instance;
     private static ArrayList<Movie> movies = new ArrayList<>();
     private final String API_KEY = "81d8f4353c2ade529071133972205017";
     private String language;
     private final String TAG = "repo";
     public static MovieRepository getInstance() {
        if (instance == null) {
            instance = new MovieRepository();
        }
        return instance;
    }
    public static ArrayList<Movie> getMovies() {
        return movies;
    }
    public MovieRepository() {
        final ArrayList<Movie> movieJSON = new ArrayList<>();
        language = "en_US";
        ApiConfig api = new ApiConfig();
        api.createInstance().loadMovieData(API_KEY, language).enqueue(new Callback<JSONResponse>() {
            @Override
            public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
                ArrayList<Movie> jsonResults = response.body().getResults();
                for (int i = 0; i < jsonResults.size() ; i++){
                    Movie movie = new Movie();
                    movie.setId(jsonResults.get(i).getId());
                    movie.setTitle(jsonResults.get(i).getTitle());
                    movie.setOverview(jsonResults.get(i).getOverview());
                    movie.setReleaseDate(jsonResults.get(i).getReleaseDate());
                    movie.setPosterPath(jsonResults.get(i).getPosterPath());
                    movieJSON.add(movie);
                }

                Log.d(TAG, "onResponse: " + jsonResults.toString());
            }

            @Override
            public void onFailure(Call<JSONResponse> call, Throwable t) {
                Log.d(TAG, "onFailure: " + t.getMessage());
            }
        });

        this.movies = movieJSON;
    }

+ MovieViewModel类

    public class MovieViewModel extends ViewModel {
    private static MutableLiveData<ArrayList<Movie>> movieData = new MutableLiveData<>();
    private static ArrayList<Movie> movies;
    public static LiveData<ArrayList<Movie>> getMoviesData() {
        return movieData;
    }
    public static MovieAdapter init(MovieAdapter adapter) {
        if (movies == null) {
            movies = MovieRepository.getInstance().getMovies();
            adapter.setMovies(movies);
        }
        return adapter;
    }

+ MovieFragment类

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        movieViewModel = ViewModelProviders.of(this).get(MovieViewModel.class);
        adapter = new MovieAdapter(getContext());
        movieViewModel.init(adapter);
        movieViewModel.getMoviesData().observe(this, new Observer<ArrayList<Movie>>() {
            @Override
            public void onChanged(@Nullable ArrayList<Movie> movies) {
                adapter.notifyDataSetChanged();
            }
        });

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_movie_list, container, false);
        if (view instanceof RecyclerView) {
            final Context context = view.getContext();
            RecyclerView recyclerView = (RecyclerView) view;
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            recyclerView.setAdapter(adapter);
        }
        return view;
    }

它总是以适配器中的NullPointer结尾(可能是因为数据无法传递到适配器)

0 个答案:

没有答案