Getting response as null in onResponse(). How to improve time duration for response?
public class RequestClass {
private static final String TAG = RequestClass.class.getSimpleName();
private final static String API_KEY = "apikey";
static ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
static public List<Movie> getTopMovies(String page) {
final List<Movie> lowDetailMovies = new ArrayList<Movie>();
Call<MoviesResponse> call = apiService.getTopRatedMovies(API_KEY, page);
call.enqueue(new Callback<MoviesResponse>() {
@Override
public void onResponse(Call<MoviesResponse> call, Response<MoviesResponse> response) {
List<DBMovie> movies = response.body().getResults();
for (DBMovie movie : movies) {
String[] genres = new String[6];
int i = 0;
for (int gen : movie.getGenreIds())
genres[i++] = gen + "";
lowDetailMovies.add(new Movie(movie.getId(), Double.toString(movie.getVote_average()), movie.getTitle(), movie.getTitle(), movie.getPoster_path(), movie.getOriginal_language(),
movie.getOriginal_title(), genres, movie.getOverview(), movie.getRelease_date(), false, false));
}
}
@Override
public void onFailure(Call<MoviesResponse> call, Throwable t) {
Log.e(TAG, t.toString());
}
});
return lowDetailMovies;
}
}
Anyone can help me with a solution to get the list after onResponse method.
The duration of the onResponse method has about 400 ms.
This is the part of the code where I need the list:
@OnClick(R.id.popular_button)
public void showPopularMovies(View view) {
List<Movie> movies;
movies = RequestClass.getTopMovies("1");
Intent intent = new Intent(MainActivity.this, VODActivity.class);
Collections.sort(movies, Util.compareByPopularityAscending);
Bundle bundle = new Bundle();
bundle.putSerializable(Constants.bundleMovieKey, (Serializable) movies);
bundle.putString(Constants.bundleVodActivityTitle, Constants.PopularMoviesTitle);
intent.putExtras(bundle);
startActivity(intent);
}
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…