Using Robospice Is There A Way To Get The Http Error Code Out Of An Exception?
Solution 1:
I looked over Spring-Android closer and it seems getRestTemplate().getForObject(...) throws a HttpClientErrorException when a 401 or any network error occurs.
Looking at the Robo Spice for where they catch that exception I found they catch it in RequestProcessor.java in the processRequest function. They pass the Spring-Android exception in as the throwable inside their SpiceException that inherits from Java exception class.
So you just do the following inside your RoboSpice RequestListener to see if it a 401 UNAUTHORIZED exception.
privateclassMyRequestListenerimplementsRequestListener<RESULT> {
publicvoidonRequestFailure( SpiceException arg0 ) {
if(arg0.getCause() instanceofHttpClientErrorException)
{
HttpClientErrorException exception = (HttpClientErrorException)arg0.getCause();
if(exception.getStatusCode().equals(HttpStatus.UNAUTHORIZED))
{
Ln.d("401 ERROR");
}
else
{
Ln.d("Other Network exception");
}
}
elseif(arg0 instanceofRequestCancelledException)
{
Ln.d("Cancelled");
}
else
{
Ln.d("Other exception");
}
};
publicvoidonRequestSuccess( RESULT result ) {
Ln.d("Successful request");
}
}
Solution 2:
I am using the google http client with RoboSpice and has the same issue but was easy to solve with request.setThrowExceptionOnExecuteError(false);
and checking the response code on the resulting HttpResponse
object
EDIT: the code snippit as requested
HttpRequestrequest= getHttpRequestFactory().buildPostRequest(newGenericUrl(URL), content);
request.setThrowExceptionOnExecuteError(false);
HttpResponseresponse= request.execute();
switch(response.getStatusCode())
{
case HttpStatusCodes.STATUS_CODE_UNAUTHORIZED:
returnnewMyBaseResponse(responseBody);
default:
thrownewRuntimeException("not implemented yet");
}
Solution 3:
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:
privatefinalclassMyRequestListenerextendsRequestListener<MyResponse> {
@OverridepublicvoidonRequestFailure(SpiceException spiceException) {
super.onRequestFailure(spiceException);
if (spiceException instanceof NetworkException) {
NetworkExceptionexception= (NetworkException) spiceException;
if (exception.getCause() instance RetrofitError) {
RetrofitErrorerror= (RetrofitError) exception.getCause();
inthttpErrorCode= 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...
Solution 4:
With google http client for java you can also intercept error responses like so:
publicstaticclassInitInterceptimplementsHttpRequestInitializer, HttpUnsuccessfulResponseHandler {
@OverridepublicbooleanhandleResponse(
HttpRequest request,
HttpResponse response,
boolean retrySupported)throws IOException {
if (response.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED) {
...
}
returnfalse;
}
@Overridepublicvoidinitialize(HttpRequest request)throws IOException {
request.setUnsuccessfulResponseHandler(this);
}
}
and in your GoogleHttpClientSpiceService:
@OverridepublicHttpRequestFactorycreateRequestFactory() {
returnAndroidHttp
.newCompatibleTransport().createRequestFactory(newInitIntercept());
}
Solution 5:
It's even easier than all of the other answers.
For me @Scrotos answer was problematic, because the whole point for the 401 to be caught is to make the request to auth endpoint and then make the primary request again. So that you only return to UI with desired data or some "real" error.
So it shouldn't be done inside the callback, but rather inside loadDataFromNetwork()
itself.
I've done it this way:
@Overridepublic SubscriptionsContainer loadDataFromNetwork() {
//...
ResponseEntity<SubscriptionsContainer> response = null;
try {
response = getRestTemplate().exchange(
//your request data
);
} catch (HttpClientErrorException e) {
HttpStatusstatusCode= e.getStatusCode();
//check the exception, if it's 401, make call to auth and repeat loadDataFromNetwork()
}
Post a Comment for "Using Robospice Is There A Way To Get The Http Error Code Out Of An Exception?"