[关闭]
@liter 2015-10-23T07:47:55.000000Z 字数 3575 阅读 3919

Android网络通信框架LiteHttp 第八节:处理异常和取消请求

litehttp2.x版本系列教程


官网: http://litesuits.com

QQ群: 大群 47357508,二群 42960650

本系列文章面向android开发者,展示开源网络通信框架LiteHttp的主要用法,并讲解其关键功能的运作原理,同时传达了一些框架作者在日常开发中的一些最佳实践和经验。

本系列文章目录总览: https://zybuluo.com/liter/note/186513


第八节:LiteHttp之处理异常和取消请求

1. 处理异常

异步请求处理异常只需要在HttpListener的onFailure中着手即可,使用异常处理器:

  1. liteHttp.executeAsync(
  2. new StringRequest("httpa://invalid-url").setHttpListener(
  3. new HttpListener<String>() {
  4. @Override
  5. public void onFailure(HttpException exception, Response<String> response) {
  6. new HttpExceptionHandler() {
  7. @Override
  8. protected void onClientException(HttpClientException e, ClientException type) {
  9. }
  10. @Override
  11. protected void onNetException(HttpNetException e, NetException type) {
  12. }
  13. @Override
  14. protected void onServerException(HttpServerException e, ServerException type, HttpStatus status) {
  15. }
  16. }.handleException(exception);
  17. }
  18. }));

2. 异常处理的最佳实践方式

下面介绍如何通过继承或者组合的方式来拓展、增强lite-http。

2.1 自定义异常处理器

推荐通过继承的手法来拓展,自建一个更强大的HttpExceptHandler来统一处理各类异常:

  1. // Best Practices of Exception Handling
  2. public class MyHttpExceptHandler extends HttpExceptionHandler {
  3. private Activity activity;
  4. public MyHttpExceptHandler(Activity activity) {
  5. this.activity = activity;
  6. }
  7. @Override
  8. protected void onClientException(HttpClientException e, ClientException type) {
  9. switch (e.getExceptionType()) {
  10. case UrlIsNull:
  11. break;
  12. case ContextNeeded:
  13. // some action need app context
  14. break;
  15. case PermissionDenied:
  16. break;
  17. case SomeOtherException:
  18. break;
  19. }
  20. HttpUtil.showTips(activity, "LiteHttp2.0", "Client Exception:\n" + e.toString());
  21. activity = null;
  22. }
  23. @Override
  24. protected void onNetException(HttpNetException e, NetException type) {
  25. switch (e.getExceptionType()) {
  26. case NetworkNotAvilable:
  27. break;
  28. case NetworkUnstable:
  29. // maybe retried but fail
  30. break;
  31. case NetworkDisabled:
  32. break;
  33. default:
  34. break;
  35. }
  36. HttpUtil.showTips(activity, "LiteHttp2.0", "Network Exception:\n" + e.toString());
  37. activity = null;
  38. }
  39. @Override
  40. protected void onServerException(HttpServerException e, ServerException type,
  41. HttpStatus status) {
  42. switch (e.getExceptionType()) {
  43. case ServerInnerError:
  44. // status code 5XX error
  45. break;
  46. case ServerRejectClient:
  47. // status code 4XX error
  48. break;
  49. case RedirectTooMuch:
  50. break;
  51. default:
  52. break;
  53. }
  54. HttpUtil.showTips(activity, "LiteHttp2.0", "Server Exception:\n" + e.toString());
  55. activity = null;
  56. }
  57. }

可以看到,增强版的 异常处理器 细分了各种异常,并且弹窗提示用户,开发者可根据产品调整细节。

2.2 自定义监听器

如果很多场景下都需要这个异常处理器来处理,那么推荐通过继承的手法来拓展HttpListener:

  1. class MyHttpListener<T> extends HttpListener<T> {
  2. private Activity activity;
  3. public MyHttpListener(Activity activity) {
  4. this.activity = activity;
  5. }
  6. // disable listener when activity is null or be finished.
  7. @Override
  8. public boolean disableListener() {
  9. return activity == null || activity.isFinishing();
  10. }
  11. // handle by this by call super.onFailure()
  12. @Override
  13. public void onFailure(HttpException e, Response response) {
  14. // handle exception
  15. new MyHttpExceptHandler(activity).handleException(e);
  16. }
  17. }
  18. liteHttp.executeAsync(new StringRequest("httpa://invalid-url")
  19. .setHttpListener(new MyHttpListener<String>(activity){
  20. @Override
  21. public void onFailure(HttpException e, Response response) {
  22. super.onFailure(e, response);
  23. // 通过调用父类的处理方法,来调用 MyHttpExceptHandler 来处理异常。
  24. }
  25. }));

经过拓展的 监听器 有两点机智的地方:

  • 默认使用自定义的 异常处理器 统一处理异常
  • 通过复写 disableListener 方法,使得在 Activity 为 null 或者 finish的情况下 监听器 的回调不再触发

不仅仅统一处理,而且可以避免页面已销毁而请求终于也成功导致各种异常的尴尬。这相当于和 Activity 的生命周期绑定了,实际开发中很实用。

3. 正确地取消请求

lite-http框架设计之初就考虑到了请求可能随时被取消,因此及时地获取和判断请求是否取消,以及获取线程的状态。当请求被取消,或者线程被中断,那么lite-http将在最短时间内结束本次请求。

取消请求的第一种方式:

  1. StringRequest stringRequest = new StringRequest(url);
  2. liteHttp.executeAsync(stringRequest);
  3. SystemClock.sleep(100);
  4. // one correct way is cancel this request
  5. stringRequest.cancel();

取消请求的第二种方式,中断线程:

  1. StringRequest stringRequest = new StringRequest(url);
  2. FutureTask futureTask = liteHttp.performAsync(stringRequest);
  3. SystemClock.sleep(100);
  4. // other correct way is interrupt this thread or task.
  5. futureTask.cancel(true);

如果是同步的请求,直接调用 Thread 的 interrupt() 即可。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注