androidretrofit2rx-java2

Retrofit2 and RxJava2 : How to detect the network error


I'm using Retofit2 and RxJava2 to fetch data from API. I am using the following code

Observable<Recipe> recipeObservable = getDataManager().getRecipes(String.valueOf(page));

        recipeObservable.subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Recipe>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Recipe recipe) {

                        getMvpView().updateRecipeList(recipe.getResults());
                        ++page;
                        //getMvpView().hideLoading();
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                        if (e instanceof HttpException)
                            Timber.d("Network Error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });

But the problem is Timber.d("Network Error"); never gets executed. I looked on various websites but could not solve the problem. I'm used to Retrofit2 but not familiar with RxJava2. What can I try next?


Solution

  • HttpException is not related to network failures. IOException is what you are looking for.