[关闭]
@liter 2015-10-23T07:49:28.000000Z 字数 4424 阅读 3987

Android网络通信框架LiteHttp 第十一节:全局配置与参数设置详解

litehttp2.x版本系列教程


官网: http://litesuits.com

QQ群: 大群 47357508,二群 42960650

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

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


第十一节:LiteHttp之全局配置与参数设置详解

lite-http可以设置全局监听器,全局默认请求方式等,便于统一进行管理和减少代码量。

可以进行的全局设置有:

关于一些名词:

全局:即对每一个请求都生效。
默认:即请求没有配置此参数时,作为其默认值配置给它。

全局默认只是备胎,也就是缺省值,如果请求本身已经设置该参数则 以其独立设置为准,无视全局配置,遵循就近原则。

看下面代码:

  1. // Detail of Configuration
  2. // init common headers for all request
  3. List<NameValuePair> headers = new ArrayList<NameValuePair>();
  4. headers.add(new NameValuePair("cookies", "this is cookies"));
  5. headers.add(new NameValuePair("custom-key", "custom-value"));
  6. HttpConfig newConfig = new HttpConfig(activity);
  7. // app context(be used to detect network and get app files path)
  8. newConfig.setContext(activity);
  9. // the log is turn on when debugged is true
  10. newConfig.setDebugged(true);
  11. // set user-agent
  12. newConfig.setUserAgent("Mozilla/5.0");
  13. // set global scheme and host to all request.
  14. newConfig.setGlobalSchemeHost("http://litesuits.com/");
  15. // common headers will be set to all request
  16. newConfig.setCommonHeaders(headers);
  17. // set default cache path to all request
  18. newConfig.setDefaultCacheDir(Environment.getExternalStorageDirectory() + "/a-cache");
  19. // set default cache expire time to all request
  20. newConfig.setDefaultCacheExpireMillis(30 * 60 * 1000);
  21. // set default cache mode to all request
  22. newConfig.setDefaultCacheMode(CacheMode.NetFirst);
  23. // set default charset to all request
  24. newConfig.setDefaultCharSet("utf-8");
  25. // set default http method to all request
  26. newConfig.setDefaultHttpMethod(HttpMethods.Get);
  27. // set default maximum redirect-times to all request
  28. newConfig.setDefaultMaxRedirectTimes(5);
  29. // set default maximum retry-times to all request
  30. newConfig.setDefaultMaxRetryTimes(1);
  31. // set defsult model query builder to all request
  32. newConfig.setDefaultModelQueryBuilder(new JsonQueryBuilder());
  33. // whether to detect network before conneting.
  34. newConfig.setDetectNetwork(true);
  35. // disable some network
  36. newConfig.setDisableNetworkFlags(HttpConfig.FLAG_NET_DISABLE_NONE);
  37. // whether open the traffic & time statistics
  38. newConfig.setDoStatistics(true);
  39. // set connect timeout: 10s, socket timeout: 10s
  40. newConfig.setTimeOut(10000, 10000);
  41. // socket buffer size: 4096
  42. newConfig.setSocketBufferSize(4096);
  43. // if the network is unstable, wait 3000 milliseconds then start retry.
  44. newConfig.setForRetry(3000, false);
  45. // set global http listener to all request
  46. newConfig.setGlobalHttpListener(null);
  47. // set maximum size of memory cache space
  48. newConfig.setMaxMemCacheBytesSize(1024 * 300);
  49. // maximum number of concurrent tasks(http-request) at the same time
  50. newConfig.setConcurrentSize(3);
  51. // maximum number of waiting tasks(http-request) at the same time
  52. newConfig.setWaitingQueueSize(100);
  53. // set overload policy of thread pool executor
  54. newConfig.setOverloadPolicy(OverloadPolicy.DiscardOldTaskInQueue);
  55. // set schedule policy of thread pool executor
  56. newConfig.setSchedulePolicy(SchedulePolicy.LastInFirstRun);
  57. // set a new config to lite-http
  58. liteHttp.initConfig(newConfig);

通过上面的方法命名大多数开发者就能猜到其功能了,因为英文注释写的比较蹩脚,还是用中文都阐述了一遍。。。

爷已经很努力地用英文为代码写注释了,但有些意思还是不能完全的表达出来,中文解释下表达的比较好,中文对国内开发者更友好。

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