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

前端 未结 5 822
失恋的感觉
失恋的感觉 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:13

    With google http client for java you can also intercept error responses like so:

    public static class InitIntercept implements 
        HttpRequestInitializer, HttpUnsuccessfulResponseHandler {
    
        @Override
        public boolean handleResponse(
            HttpRequest request, 
            HttpResponse response, 
            boolean retrySupported) throws IOException {
    
            if (response.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED) {
                ...
            }
    
            return false;
        }
    
        @Override
        public void initialize(HttpRequest request) throws IOException {
            request.setUnsuccessfulResponseHandler(this);
        }
    }
    

    and in your GoogleHttpClientSpiceService:

    @Override
    public HttpRequestFactory createRequestFactory() {
        return AndroidHttp
            .newCompatibleTransport().createRequestFactory(new InitIntercept());
    }
    

提交回复
热议问题