Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get https status code in Retrofit RxJava Android?

Before I always used retrofit to load data. It's very easy to handle or get status code of the response.

But now I want to use Retrofit with RxJava but I don't know how to handle or get the https status code of the response in onNext method.

progressDialog.show();

Observable<ResponseData> observable = apiService.getData();
compositeDisposable.add(observable
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeWith(new DisposableObserver<ResponseData>() {
            @Override
            public void onNext(ResponseData responseData) {
                Log.e("pppp", "onNext: " + responseData.toString());
            }

            @Override
            public void onError(Throwable e) {
                progressDialog.dismiss();
                Log.e("pppp", "onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {
                progressDialog.dismiss();
                Log.e("pppp", "onComplete");
            }
        })
);

Everyone, please help me to solve this problem.
Thanks!

like image 242
Vichit Avatar asked Jan 03 '23 09:01

Vichit


2 Answers

You should wrap your ResponseData inside Response as

Observable<Response<ResponseData>> observable = apiService.getData(); 

Then inside onNext

@Override 
public void onNext(Resposne<ResponseData> response) {
   int statusCode = response.code();
}

and for error

@Override 
public void onError(Throwable e) {
    ((HttpException) e).code();
}
like image 52
Dhiraj Sharma Avatar answered Jan 20 '23 19:01

Dhiraj Sharma


responseData.code gives you the status code

   int statusCode = responseData.code();
like image 25
Sushil Chaudhary Avatar answered Jan 20 '23 19:01

Sushil Chaudhary