异常统一处理的一些思路
来源:互联网 发布:电脑怎么删除软件 编辑:程序博客网 时间: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
- 异常统一处理的一些思路
- 统一的处理异常
- 异常的统一处理
- 统一异常的处理
- 统一的异常处理
- 统一处理jersey的异常
- springMVC的统一异常处理
- springmvc统一的异常处理
- 数据库连接异常的处理思路
- 关于合理使用SpringMVC统一异常处理机制以改善代码风格的一些思考
- resteasy 统一的异常处理handler
- c/c++的异常统一处理
- Jersey框架的统一异常处理机制
- Spring MVC的异常统一处理方法
- spring中对异常的统一处理
- Spring对异常的统一处理
- Jersey框架的统一异常处理
- spring mvc对异常的统一处理
- Android 多线程编程
- 基于centos6.8的Suricata+Barnyard2的IDS前端Snorby
- 《C++ Primer Plus(第六版)》(3)(第四章 复合类型 笔记)
- **linux配置tomcat以service方式启动**
- erp---tiptop内置函数
- 异常统一处理的一些思路
- 怎么学汇编---第一个程序
- 欢迎使用CSDN-markdown编辑器
- 边缘检测:Canny算子
- 阴影
- &&与&之间的区别
- 【ubuntu14.04配置caffe】二——安装intel MKL
- Android View绘制流程完全解析(三)
- assertThat