Search code examples
androidretrofit2rx-java2http-error

Retrofit/RxJava - get http response code


Android, Retrofit, RxJava. Please look at this example call:

 mcityService.signOut(signOutRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(resp ->
                {
                    busyIndicator.dismiss();
                    finish();
                }, throwable ->
                {
                    Log.d(tag, throwable.toString());
                    busyIndicator.dismiss();
                    Toast.makeText(this,throwable.getMessage(),Toast.LENGTH_LONG).show();
                    finish();
                });

Does anybody know how to get error code (as error number) from throwable? I am able to get full stacktrace or message (as shown above). How to capture error code?

Error code from throwable: enter image description here


Solution

  • Just use casting??

    mcityService.signOut(signOutRequest)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(resp ->
                    {
                        busyIndicator.dismiss();
                        finish();
                    }, throwable ->
                    {
                        Log.d(tag, throwable.toString());
                        Log.d(code, ((HttpException)throwable).code());
    
                        busyIndicator.dismiss();
                        Toast.makeText(this,throwable.getMessage(),Toast.LENGTH_LONG).show();
                        finish();
                    });