How to handle onError inside RxJava. I am getting “OnErrorNotImplementedException”

前端 未结 3 1246
暖寄归人
暖寄归人 2021-01-18 05:50

In my app I am using ReactiveLocationProvider library (link). I subscribe for updates in the onCreate method. It works fine when the device is online but if I switch my wifi

相关标签:
3条回答
  • 2021-01-18 05:55

    There is an error happening when you are trying to geocode the location.

    Caused by: java.io.IOException: Timed out waiting for response from server at android.location.Geocoder.getFromLocation(Geocoder.java:136)

    When you call subscribe with one Action1 as a parameter it only handles calls to onNext and if an error happens the app will crash.

    You need to subscribe with:

    subscribe(new Subscriber<Location>() {
        @Override
        public void onNext(Location location) { /*Handle the location updates*/ }
    
        @Override
        public void onCompleted() { }
    
        @Override
        public void onError(Throwable e) { }
    })
    
    0 讨论(0)
  • 2021-01-18 06:01

    Consider subscribing to the location data with subscribe(Action<T> onNext, Action<Throwable> onError). In other words, you should handle errors that are emitted from the observable data.

    0 讨论(0)
  • 2021-01-18 06:14

    If you still wish to implement only one method and handle error too, then probably you can have abstract CustomSubscriber like below.

    public abstract class CustomSubscriber<T> extends Subscriber<T> {
    
        @Override
        public void onCompleted() {
    
        }
    
        @Override
        public void onError(Throwable e) {
    
        }
    
        @Override
        public void onNext(T t) {
    
        }
    }
    

    Implement this way

     getObservable()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new CustomSubscriber<List<String>>() {
            @Override
            public void onNext(List<String> list) {
    
            }
        });
    

    hope some one will benefited.

    0 讨论(0)
提交回复
热议问题