Using RoboSpice is there a way to get the HTTP Error Code out of an exception?

前端 未结 5 885
失恋的感觉
失恋的感觉 2021-01-31 11:29

I am writing an application that uses RoboSpice. In the request listener onRequestFailure( SpiceException arg0 ) is there a way to know for sure that the error was a result of a

5条回答
  •  长情又很酷
    2021-01-31 12:25

    For those who can't resolve HttpClientErrorException into a type, and cannot find any documentations online, (that's me), here is my approach:

    In my fragment, here is my listener:

    private final class MyRequestListener extends RequestListener {
    
      @Override
      public void onRequestFailure(SpiceException spiceException) {
        super.onRequestFailure(spiceException);
        if (spiceException instanceof NetworkException) {
          NetworkException exception = (NetworkException) spiceException;
          if (exception.getCause() instance RetrofitError) {
            RetrofitError error = (RetrofitError) exception.getCause();
            int httpErrorCode = error.getResponse().getStatus();
            // handle the error properly...
            return;
          }
        }
        // show generic error message
      }
    
    }
    

    Hope this maybe helpful to someone.

    I would move the whole if clause into a static function so it can be reused. Just return 0 if exception doesn't match. And I haven't verify if any of the casting can be removed...

提交回复
热议问题