使用改造和 rxJava 重复 api 调用
Repeat api call using retrofit and rxJava
我是 RxJava 的新手,我想使用改造重复(3 秒)API 调用。我看到了一些答案,但我不知道如何实现。
我按照这个重复调用。
这是我的代码
apiInterface = ApiClient.getClient().create(ApiInterface.class);
apiInterface.getTopRatedMovie(API_KEY)
.interval(3, TimeUnit.SECONDS)
.flatMap(n -> apiInterface.getTopRatedMovie(API_KEY))
.repeat()
.subscribe(new Observer<MovieResopnse>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onNext(@NonNull MovieResopnse movieResopnse) {
movies = movieResopnse.getResults();
Log.d(TAG, "onResponse: Number of movies received: " + movies.size());
tv_count.setText("Movie: " + count);
count++;
Picasso.get()
.load(IMAGE_URL + movies.get(item).getPosterPath())
.into(image);
title.setText(movies.get(item).getOriginalTitle());
subtitle.setText(movies.get(item).getTitle());
description.setText(movies.get(item).getOverview());
rating.setText("" + movies.get(item).getVoteAverage());
release_date.setText("Release: " + movies.get(item).getReleaseDate());
}
@Override
public void onError(@NonNull Throwable e) {
Log.d(TAG, "onError: " + e.getMessage());
Toast.makeText(MainActivity.this, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete() {
}
});
以下是我如何使用改造进行重复调用
compositeDisposable.add(Observable.interval(6, TimeUnit.SECONDS)
.flatMap(n -> apiInterface.getTopRatedMovie(API_KEY))
.repeat()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<MovieResopnse>() {
@Override
public void accept(MovieResopnse movieResopnse) throws Exception {
movies = movieResopnse.getResults();
Log.d(TAG, "onResponse: Number of movies received: " + movies.size());
doUIoperation();
}
}, this::onError));
我是 RxJava 的新手,我想使用改造重复(3 秒)API 调用。我看到了一些答案,但我不知道如何实现。
我按照这个
这是我的代码
apiInterface = ApiClient.getClient().create(ApiInterface.class);
apiInterface.getTopRatedMovie(API_KEY)
.interval(3, TimeUnit.SECONDS)
.flatMap(n -> apiInterface.getTopRatedMovie(API_KEY))
.repeat()
.subscribe(new Observer<MovieResopnse>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onNext(@NonNull MovieResopnse movieResopnse) {
movies = movieResopnse.getResults();
Log.d(TAG, "onResponse: Number of movies received: " + movies.size());
tv_count.setText("Movie: " + count);
count++;
Picasso.get()
.load(IMAGE_URL + movies.get(item).getPosterPath())
.into(image);
title.setText(movies.get(item).getOriginalTitle());
subtitle.setText(movies.get(item).getTitle());
description.setText(movies.get(item).getOverview());
rating.setText("" + movies.get(item).getVoteAverage());
release_date.setText("Release: " + movies.get(item).getReleaseDate());
}
@Override
public void onError(@NonNull Throwable e) {
Log.d(TAG, "onError: " + e.getMessage());
Toast.makeText(MainActivity.this, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete() {
}
});
以下是我如何使用改造进行重复调用
compositeDisposable.add(Observable.interval(6, TimeUnit.SECONDS)
.flatMap(n -> apiInterface.getTopRatedMovie(API_KEY))
.repeat()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<MovieResopnse>() {
@Override
public void accept(MovieResopnse movieResopnse) throws Exception {
movies = movieResopnse.getResults();
Log.d(TAG, "onResponse: Number of movies received: " + movies.size());
doUIoperation();
}
}, this::onError));