LoginSignup
12
12

More than 5 years have passed since last update.

Retrofit + RxAndroidでエラーコードを取得する

Posted at

Retrofit + RxAndroidを利用した際のステータスコードは
正常(200〜299)の場合はRetrofit内の処理で切り捨てられるが
エラーの場合はThrowable内にラッピングされて返却されてくる。

.getApi()
.subscribe(
    success -> //正常処理,
    e -> {
        //エラー
        if(e.getCause() instanceof RetrofitError){
            RetrofitError error = (RetrofitError)e.getCause();
            Timber.e("status = " + error.getResponse().getStatus() + ", message = " + error.getMessage());
        }else {
            Timber.e(e, e.getMessage());
        }
    }
);
12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12