0

I have ServiceB which relies on the response of service A, ServiceB gets triggered but now I hit the error of NetworkOnMainThreadException @ onError. I'm new to RxJava and just starting to try out on examples. Please help, thanks.

        ServiceA
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .flatMap(new Function<ResponseA, Observable<ResponseB>>() {
            @Override
            public Observable<Execution> apply(ResponseA responseA) throws Exception {
                return ServiceB(responseA.some_id); #ServiceB gets triggered here
            }
        })
        .subscribe(new Observer<ResponseB>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onComplete() {
            }

            @Override
            public void onError(Throwable t) {
               #NetworkOnMainThreadException!
            }

            @Override
            public void onNext(ResponseB responseB) {

            }
        });
djreenykev
  • 143
  • 2
  • 14

1 Answers1

0

a NetworkOnMainThreadException will happen when your trying to connect to a url from the UI thread , in your case you need to put the stacktrace in the question , please note that a Service will run on the Thread that was created from , so if you call it from UI thread , the service will run on the UI thread . to avoid the error from happening many methods you can use , including AsyncTask

AsyncTask.execute(new Runnable(

     @Override
     public void onRun(){
       //do the network job here
     }
 });
Reza
  • 321
  • 2
  • 4
  • 14