问题描述
我写一个使用RoboSpice的应用程序。在请求收听onRequestFailure(SpiceException为arg0)是有没有办法肯定知道错误是发生了一个401 HTTP错误的结果呢?
我有一个后台服务,当令牌到期返回401错误,则会出现我需要提示用户重新输入他们的凭据。
反正是有知道具体发生了401 HTTP错误?
下面是我的要求的例子。
公共类Looku prequest延伸SpringAndroidSpiceRequest<产品> {
公共字符串searchText;
公共字符串搜索模式;
公共Looku prequest(){
超(Product.class);
}
@覆盖
公共产品loadDataFromNetwork()抛出异常{
字符串URL =的String.Format(%S /查找S =%S&放大器; M =%s时,Constants.BASE_URL,searchText,搜索模式);
Ln.d(调用URL:%S,网址);
返回getRestTemplate()getForObject(URL,Product.class)。
}
我看着弹簧的Android更接近,似乎getRestTemplate()。getForObject(...)抛出HttpClientErrorException当401或网络错误发生。
综观机器人香料为他们捕获了异常,我发现他们抓住它RequestProcessor.java中的processRequest功能。他们传递他们的SpiceException内抛出一个继承自Java异常类弹簧的Android例外。
所以,你只需做你的RoboSpice RequestListener里面以下,看它是否一个401未授权异常。
私有类MyRequestListener实现RequestListener<结果> {
公共无效onRequestFailure(SpiceException为arg0){
如果(arg0.getCause()的instanceof HttpClientErrorException)
{
HttpClientErrorException异常=(HttpClientErrorException)arg0.getCause();
如果(exception.getStatus code()。等于(HttpStatus.UNAUTHORIZED))
{
Ln.d(401错误);
}
其他
{
Ln.d(其他网络异常);
}
}
否则,如果(arg0中的instanceof RequestCancelledException)
{
Ln.d(取消);
}
其他
{
Ln.d(其他异常);
}
};
公共无效onRequestSuccess(RESULT结果){
Ln.d(请求成功);
}
}
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 401 HTTP Error occurred?
I have a back end service, that returns a 401 error when a token expires, when that occurs I need to prompt the user to re-enter their credentials.
Is there anyway to know that a 401 HTTP error specifically occurred?
Below is an example of my request.
public class LookupRequest extends SpringAndroidSpiceRequest <Product> {
public String searchText;
public String searchMode;
public LookupRequest() {
super( Product.class );
}
@Override
public Product loadDataFromNetwork() throws Exception {
String url = String.format("%s/Lookup?s=%s&m=%s", Constants.BASE_URL, searchText, searchMode);
Ln.d("Calling URL: %s", url);
return getRestTemplate().getForObject(url, Product.class );
}
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.
private class MyRequestListener implements RequestListener<RESULT> {
public void onRequestFailure( SpiceException arg0 ) {
if(arg0.getCause() instanceof HttpClientErrorException)
{
HttpClientErrorException exception = (HttpClientErrorException)arg0.getCause();
if(exception.getStatusCode().equals(HttpStatus.UNAUTHORIZED))
{
Ln.d("401 ERROR");
}
else
{
Ln.d("Other Network exception");
}
}
else if(arg0 instanceof RequestCancelledException)
{
Ln.d("Cancelled");
}
else
{
Ln.d("Other exception");
}
};
public void onRequestSuccess( RESULT result ) {
Ln.d("Successful request");
}
}
这篇关于使用RoboSpice有没有办法得到HTTP错误code一个异常呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!