5

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!

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Vichit
  • 309
  • 1
  • 3
  • 16
  • 1
    Possible duplicate of [Get response status code using Retrofit 2.0 and RxJava](https://stackoverflow.com/questions/33774940/get-response-status-code-using-retrofit-2-0-and-rxjava) – Milind Mevada May 11 '18 at 05:22

3 Answers3

10

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();
}
Dhiraj Sharma
  • 4,364
  • 24
  • 25
  • you sure there is no adapter/method/object able to get that instead of wrapping ResponseData object into a Response one? – mantc_sdr Dec 10 '20 at 11:55
1

responseData.code gives you the status code

   int statusCode = responseData.code();
0

for getting status

  Observable<ResponseData> observable = apiService.getData(); 
    compositeDisposable.add(observable 
            .subscribeOn(Schedulers.io()) 
            .observeOn(AndroidSchedulers.mainThread()) 
            .subscribeWith(new DisposableObserver<ResponseData>() { 
                @Override 
                public void onNext(ResponseData responseData) {
                    int statusCode = responseData.code();
                    // here you get your status code
                    Log.e("statusCode ", "onNext: " + statusCode );
                } 

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

                @Override 
                public void onComplete() { 
                    progressDialog.dismiss(); 
                    Log.e("pppp", "onComplete");
                } 
            }) 
    ); 
Basi
  • 3,009
  • 23
  • 28