0

I'm trying to handle errors in Rx android networking but unable to do so can some help.

 getCompositeDisposable().add(getManager().callApi(request)
        .subscribeOn(Schedulers.io())
        .observeOn(Schedulers.ui())
        .subscribe(apiResponse -> Log.d(TAG,"success"),
                throwable -> Log.e(TAG,throwable.getMessage)));

I want to handle errors in subscribe()

Logs:

 io.reactivex.exceptions.OnErrorNotImplementedException: java.net.ConnectException: Failed to connect 
 at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
 at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:701)
 at io.reactivex.internal.observers.ConsumerSingleObserver.onError(ConsumerSingleObserver.java:47)
 at io.reactivex.internal.operators.single.SingleObserveOn$ObserveOnSingleObserver.run(SingleObserveOn.java:79)
 at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
 at android.os.Handler.handleCallback(Handler.java:739)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:158)
 at android.app.ActivityThread.main(ActivityThread.java:7231)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 Caused by: com.androidnetworking.error.ANError: java.net.ConnectException: Failed to connect to /192.168.1.42:8484
 at com.rx2androidnetworking.Rx2InternalNetworking$SimpleANObservable.subscribeActual(Rx2InternalNetworking.java:235)
 at io.reactivex.Observable.subscribe(Observable.java:10981)
 at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual(ObservableSingleSingle.java:35)
 at io.reactivex.Single.subscribe(Single.java:2801)
 at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
 at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
 at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
 at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Harmeet Kaur
  • 154
  • 14

1 Answers1

0

Unfortunately i can not comment. Could you be more specific? What error have you got? The common way to handle errors using rx looks like this:

 Subscription subscribe = apiServices.endpointCall(...parameters...)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .unsubscribeOn(Schedulers.io());
            .subscribe(response -> {
                //do something with response
            }, throwable -> {
                //do something with error
            });

Where apiService is created by retrofit using

 service = retrofit.create(ApiServices.class);
Ikazuchi
  • 433
  • 2
  • 12
  • I am pretty sure that my example should works, but if you have still problems with that you can try this solution https://stackoverflow.com/a/34520290, under that you can find example using templates – Ikazuchi Jun 01 '18 at 08:59