Skip to content Skip to sidebar Skip to footer

Networkonmainthreadexception Using Okhttp With Rxjava2

I am trying to learn Rxjava2. I am facing a problem i.e, NetworkOnMainThreadException during network call using okHttp. I have to use only okHttp libary. This is my method where I

Solution 1:

You're calling service.getData(httpParamObject) in the main thread and passing that result to Observable.just. So your subscribeOn has no effect.

Check the documentation of Observable.create and use it instead of Observable.just

Solution 2:

just remove Observable.just() as follows

  service.getData(httpParamObject)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe((Consumer<? super Object>) getObserver());

Post a Comment for "Networkonmainthreadexception Using Okhttp With Rxjava2"