JLa*_*kin 14 android http-status-code-401 robospice
我正在编写一个使用RoboSpice的应用程序.在请求侦听器onRequestFailure(SpiceException arg0)中是否有一种方法可以确定错误是由于发生了401 HTTP错误?
我有一个后端服务,当令牌到期时返回401错误,当发生这种情况时,我需要提示用户重新输入他们的凭据.
反正知道发生了401 HTTP错误吗?
以下是我的请求示例.
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 );
}
Run Code Online (Sandbox Code Playgroud)
JLa*_*kin 20
我查看了Spring-Android close,看起来getRestTemplate().getForObject(...)在发生401或任何网络错误时抛出HttpClientErrorException.
看看Robo Spice他们捕获异常的地方,我发现他们在processRequest函数的RequestProcessor.java中捕获它.它们将Spring-Android异常作为继承自Java异常类的SpiceException中的throwable传递.
因此,您只需在RoboSpice RequestListener中执行以下操作,以查看它是否为401 UNAUTHORIZED异常.
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");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9602 次 |
| 最近记录: |