异常统一处理的一些思路

来源:互联网 发布:电脑怎么删除软件 编辑:程序博客网 时间:2024/05/01 02:50

1.AppException 统一处理所有异常

//将出现的所有异常都用AppExceptionpublic 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和RequestTaskpublic 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