1

Is there anyway to retry a retrofit http request when network connection available with Rx-java?

This is my request method

public DisposableObserver<List<Photo>> getNewPhotos(final int page,
                                                    int perPage,
                                                    String orderBy,
                                                    ObservableTransformer<List<Photo>, List<Photo>> observableTransformer,
                                                    final Callback<List<Photo>> callBack) {
    return photoService.getPhotos(page, perPage, orderBy)
            .compose(observableTransformer)
            .retryWhen(new Function<Observable<Throwable>, ObservableSource<?>>() {
                @Override
                public ObservableSource<?> apply(Observable<Throwable> throwableObservable) throws Exception {
                    return null;
                }
            })
            .onErrorResumeNext(new Function<Throwable, ObservableSource<? extends List<Photo>>>() {
                @Override
                public ObservableSource<? extends List<Photo>> apply(Throwable throwable) throws Exception {
                    return Observable.error(throwable);
                }
            })
            .subscribeWith(new DisposableObserver<List<Photo>>() {
                @Override
                public void onNext(List<Photo> value) {
                    callBack.onSuccess(value);
                }

                @Override
                public void onError(Throwable e) {
                    callBack.onError(new NetworkError(e));
                }

                @Override
                public void onComplete() {

                }
            });
}

i think maybe i could do something in retryWhen() method. i want retrofit to trigger for internet connection and retry the last request when the connection is back. i know the traditional way to retry but i think there must be a method or something in Rx-java to handle this. if someone knows its good to share it with me.

Alireza
  • 183
  • 1
  • 10
  • check this http://kevinmarlow.me/better-networking-with-rxjava-and-retrofit-on-android/. of course you need to change func1 to Function in RxJava2 – Raghunandan Oct 15 '17 at 17:26
  • you might want to have a look at [this question](https://stackoverflow.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android?noredirect=1&lq=1). But to be honest you should probably check your permissions and internet access before you start making requests. I would stick with the exponential back of examples. – Lukasz Oct 19 '17 at 13:24

0 Answers0