Android网络开发-请求队列
因为之前参与的网络开发项目都遇到一些相同的问题:
1.大量的并发请求造成堵塞,特别是遇上让人无语的3G网络,无限loading。。。
2.一般来说一个网络请求都会用使用到一个异步线程,大量的线程创建、运行、销毁又造成了系统资源的浪费
3.请求结束得到结果后,如果需要更新UI,一个不小心忘了返回UI线程,各种崩溃。。。
前些日子跟同事商量能不能做个请求队列去进行控制,于是趁着热度没消退说干就干,建了个模型,以备日后使用。
在这个模型中,有高中低三个优先级信道如下:高优先级--1,中优先级--3,低优先级--2
规则:
1.正常情况下各个优先级使用各自信道(线程)
2.高级信道满载、中、低级信道空置,则高级请求可使用低级信道
构思:
UI线程将期望的网络请求url和参数通过一个封装好的Runnable提交给Service处理(当然也可以交给一个Thread处理,本例使用Service),Service接收到请求,判断优先级,加入到相应线程池中排队。线程池启动线程发起网络请求,最后通过易做图将结果返回给Service,Service发送广播通知UI线程,UI线程更新相关界面,结束。
废话说完,上例子:
首先是封装好的Runnable
1. public class HttpConnRunnable implements Runnable, Parcelable {
2.
3. public static final int HIGH_LEVEL = 0;
4. public static final int NORMAL_LEVEL = 1;
5. public static final int LOW_LEVEL = 2;
6.
7. private int mPriority = NORMAL_LEVEL;//优先级,默认为普通
8. private String mUrl = "";
9.
10. private HttpConnListener mListener;//易做图
11.
12.
13. public HttpConnRunnable() {
14. super();
15. }
16.
17. public HttpConnRunnable(int priority) {
18. super();
19. mPriority = priority;
20. }
21.
22. @Override
23. public void run() {
24. Log.i(Thread.currentThread().getName(), "----Start to connect:" + mUrl + ", priority:" + mPriority + "-----");
25. try {
26. Thread.sleep(10000);
27. //TODO:进行网络请求相关操作,并通过listener返回结果
28. mListener.onSucceed("Connected to " + mUrl + " succeed!");
29. }
30. catch (InterruptedException e) {
31. e.printStackTrace();
32. }
33. Log.i(Thread.currentThread().getName(), "----Finish to connect:" + mUrl + ", priority:" + mPriority + "-----");
34. }
35.
36. public int getPriority() {
37. return mPriority;
38. }
39.
40. public void setPriority(int priority) {
41. mPriority = priority;
42. }
43.
44. public String getURL() {
45. return mUrl;
46. }
47.
48. public void setURL(String url) {
49. mUrl = url;
50. }
51.
52. public void setHttpConnListener(HttpConnListener listener) {
53. mListener = listener;
54. }
55.
56. //序列化,为了传递给Service,如果是使用Thread处理本例,则无需序列化
57. public static final Parcelable.Creator<HttpConnRunnable> CREATOR = new Creator<HttpConnRunnable>() {
58. @Override
59. public HttpConnRunnable createFromParcel(Parcel source) {
60. HttpConnRunnable data = null;
61. Bundle bundle = source.readBundle();
62. if(bundle != null) {
63. data = new HttpConnRunnable(bundle.getInt("PRIORITY"));
64. data.mUrl = bundle.getString("URL");
65. }
66. return data;
67. }
68.
69. @Override
70. public HttpConnRunnable[] newArray(int size) {
71. return new HttpConnRunnable[size];
72. }
73. };
74.
75. @Override
76. public int describeContents() {
77. return 0;
78. }
79.
80. @Override
81. public void writeToParcel(Parcel dest, int flags) {
82. Bundle bundle = new Bundle();
83. bundle.putInt("PRIORITY", mPriority);
84. bundle.putString("URL", mUrl);
85. dest.writeBundle(bundle);
86. }
87.
88. }
Service的处理:
1. public class HttpConnService extends Service implements HttpConnListener {
2. public static final String HTTP_POOL_PARAM_KEYWORD = "HttpPoolParam"; //网络参数传递的关键字
3.
4. private final int HIGH_POOL_SIZE = 1;
5. private final int NORMAL_POOL_SIZE = 3;
6. private final int LOW_POOL_SIZE = 2;
7.
8. // 可重用固定线程数的线程池
9. &n
补充:移动开发 , Android ,