异常统一处理的一些思路

1.AppException 统一处理所有异常

//将出现的所有异常都用AppException
public class AppException extends Exception {

    public int statusCode;
    public String responseMessage;

    public AppException(int status, String responseMessage) {
        super(responseMessage);
        this.statusCode = status;
        this.responseMessage = responseMessage;
    }

    public AppException(String detailMessage) {
        super(detailMessage);
    }
}

    //像这样替换
     @Override
    public T parse(HttpURLConnection connection,OnProgressUpdatedListener listener) throws AppException {
        try {
            int status = connection.getResponseCode();
            if (status == HttpStatus.SC_OK) {
               ......
            }else {
                //都看作是联网失败
                throw new AppException(status,connection.getResponseMessage());
            }
        } catch (Exception e) {
            throw new AppException(e.getMessage());
        }
    }

2.常见的异常

所有异常
参数错误,403
服务器错误,500
服务器返回json格式不正确,导致反序列化不成功

APPException还可以加在其它地方,eg,请求之前判断URL是否正确
if (!URLUtil.isNetworkUrl(request.url)) {
        throw new AppException("the url :" + request.url + " is not valid");
    }
//public static boolean isNetworkUrl(String url) {
    if (url == null || url.length() == 0) {
        return false;
    }
    return isHttpUrl(url) || isHttpsUrl(url);
}

3.token 失效的解决思路

考虑这样一种场景,当User登录之后很长一段时间都不用登录
方案很多:cookie、session、token
这里看一下 token:

当User登录之后,服务器会返回一个token的字符串,这个字符串是一个UserID + 其它信息的一个MD5
把这个token记录下来,以后所有的请求都会加上这个token,把它解密出来判断是否失效

没有失效,回取出对应ID的所有数据
失效,返回403 + token 失效

//框架里面会写接口,还有请求队列中,我的是Request和RequestTask
public interface OnGlobalExceptionListener {

    //处理类似 token 失效这类的Exception,错误代码403
    boolean handleException(AppException exception);
}

//Request
  public OnGlobalExceptionListener onGlobalExceptionListener;

  public void setGlobalExceptionListener(OnGlobalExceptionListener onGlobalExceptionListener) {
        this.onGlobalExceptionListener = onGlobalExceptionListener;
    }

//RequestTask
    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        if (o instanceof AppException) {
            if (request.onGlobalExceptionListener != null){
                if(!request.onGlobalExceptionListener.handleException((AppException)o)){
                    request.iCallback.onFailure((AppException) o);
                }
            }
        } else {
            request.iCallback.onSuccess(o);
        }
    }

4.自己处理

public class BaseActivity extends ActionBarActivity implements OnGlobalExceptionListener {
    @Override
    public boolean handleException(AppException e) {
        if (e.statusCode == 403) {//服务器拒绝访问
            if("token invalid".equals(e.responseMessage)) {
//                        TODO relogin
                return true;
            }
        }
        return false;
    }
}
 @Override
 public void onFailure(AppException e) {
     if (e.statusCode == 403) {
        if ("password incorrect".equals(e.responseMessage)) {
//                        TODO
        } else if ("token invalid".equals(e.responseMessage)) {
//                        TODO relogin
        }
     }
     e.printStackTrace();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值