TheJudge TheJudge - 23 days ago 7
Android Question

RxJavaCallAdapter and Result Codes

I'm wondering whether there's a way to work with result codes in RxJava + Retrofit2. Let's say I have a call

api.getAll()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(data -> {
// Do something
});


and I'd like to check result code of the call. And I don't mean just error handling, I'm talking about 204 NO CONTENT for example. I haven't found anything here on Google so any help is appreciated. Thanks

Answer

declare the api call like this

Observable<Response<MyResponseObject>> apiCall(@Body body);

and in subscriber do like this...

    apiCall.getAll()
       .subscribeOn(Schedulers.io())
       .observeOn(AndroidSchedulers.mainThread())
       .subscribe(new Observer<Response<MyResponseObject>>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(Response<MyResponseObject>> response) {
                           //get response code like
                           if(response.code()==202){

                        }
                    });
Comments