我正在尝试一个遵循MVVM架构的应用程序,其中我需要从网络中获取JSON文件,我试图实现翻新和Volley,在“第一按钮”上单击“翻新”将用于填充RecyclerView,而在其他按钮Volley应该做同样的事情。
我曾尝试在ViewModel类中实现这两个调用,但只有翻新可以工作。
public class MainViewModel extends ViewModel {
public void setmContext(Context mContext) {
this.mContext = mContext;
}
private MutableLiveData<List<Movie>> movieList;
private MutableLiveData<Boolean> isLoading;
private MovieService movieService;
private VolleyService volleyService;
private JsonArrayRequest ArrayRequest ;
private RequestQueue requestQueue ;
private Context mContext;
public MainViewModel(MovieService movieService) {
this.movieService = movieService;
movieList = new MutableLiveData<>();
isLoading = new MutableLiveData<>();
}
public MainViewModel() {
}
MutableLiveData<List<Movie>> getMovies() {
return movieList;
}
MutableLiveData<Boolean> getLoadingStatus() {
return isLoading;
}
void loadMoviesNetwork() {
setIsLoading(true);
Call<MovieResponse> movieCall = movieService.getMovieApi().getAllMovie();
movieCall.enqueue(new MovieCallback());
}
void loadMovieLocal() {
setIsLoading(true);
String name = "Breaking Bad";
String image = "https://coderwall-assets-0.s3.amazonaws.com/uploads/picture/file/622/breaking_bad_css3_svg_raw.png";
String name2= "Sherlock";
String image2= "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N//AABEIAF0AnQMBIgACEQEDEQH/xAAbAAADAQEBAQEAAAAAAAAAAAAEBQYDAgcBAP/EAD8QAAIBAwMBBAcFBQYHAAAAAAECAwAEEQUSITEGE0FRIjJhcYGRsRQjocHRFUKy4fAWJDNSYvEmQ1NjcoKi/8QAGAEAAwEBAAAAAAAAAAAAAAAAAQIDAAT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207S65MrdIo1A+PJ+tav0G/yTVza/YZUIyY5l6gdKb6FbNImSCFDZzR6QI9yiuoYRhNoPv/AJUbCBHGMD1qIoNOneXb5JARFTI8M8n8qw2Ws3+FESi8AonB/WswWnuW3MQZpmj4PCqBz8TjrWjSSM7RwFYkiO0DbnNGjH//2Q==";
List<Movie> movies = new ArrayList<>();
movies.add(new Movie(name,image,name));
movies.add(new Movie(name2,image2,name2));
movies.add(new Movie(name,image,name));
setMovies(movies);
}
void showEmptyList() { setMovies(Collections.<Movie>emptyList()); }
private void setIsLoading(boolean loading)
{
isLoading.postValue(loading);
}
private void setMovies(List<Movie> movies) {
setIsLoading(false);
movieList.postValue(movies);
}
/**
* Callback
**/
private class MovieCallback implements Callback<MovieResponse> {
@Override
public void onResponse(@NonNull Call<MovieResponse> call, @NonNull Response<MovieResponse> response) {
MovieResponse movieResponse = response.body();
if (movieResponse != null) {
setMovies(movieResponse.getMovies());
} else {
setMovies(Collections.<Movie>emptyList());
}
}
@Override
public void onFailure(Call<MovieResponse> call, Throwable t) {
/* setMovies(Collections.<Movie>emptyList());*/
try{
call.clone();
}
catch (Exception e)
{
setMovies(Collections.<Movie>emptyList());
}
}
}
void loadMovieVolley()
{
setIsLoading(true);
final List<Movie> mMovie = new ArrayList<>();
requestQueue=Volley.newRequestQueue(mContext);
ArrayRequest = new JsonArrayRequest(Constants.URL+"movies/", new com.android.volley.Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject ;
for (int i = 0 ; i<response.length();i++) {
try {
jsonObject = response.getJSONObject(i);
Movie movie = new Movie();
movie.setDescription(jsonObject.getString("description"));
movie.setTitle(jsonObject.getString("title"));
movie.setImage((jsonObject.getString("image")));
mMovie.add(movie);
}
catch (JSONException e) {
e.printStackTrace();
}
}
setMovies(mMovie);
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//requestQueue = Volley.newRequestQueue();
requestQueue.add(ArrayRequest);
}
// setMovies(Collections.<Movie>emptyList());
}