Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
708 views
in Technique[技术] by (71.8m points)

android - The response from the retrofit call is too slow and it returns a null list

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I used ReactiveX to solve the problem. Another solution was a callback from onResponse() method.

    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
    apiService.getTopRatedMovies(Constants.API_KEY, Constants.FIRST_PAGE)
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(popularMoviesSubscriber);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...