GalHttprequest 是一个android平台上一个轻量级的http网络请求及缓存框架。

当前GalHttpRequest支持以下功能:

同步请求Stirng、InputStream、Bitmap;
异步请求String、InputStream、Bitmap;支持回调接口;
支持异步下载文件,提供监听进度回调接口;
支持缓存参数设置;
支持多线程及队列请求;
自动适配移动、联通、电信wap代理;
支持快捷post请求;

项目如图:


android网络请求封装实现 android网络请求缓存_Android

效果如图:


android网络请求封装实现 android网络请求缓存_android网络请求封装实现_02

android网络请求封装实现 android网络请求缓存_框架_03


android网络请求封装实现 android网络请求缓存_缓存_04

主要部分就十一个目标文件,其定义如下:


1. //解析日期的日期和时间(RFC822和W3CDateTime的格式字符串)  
2. public class DateParser

1. 主要部分就十一个目标文件,其定义如下:  
2. //解析日期的日期和时间(RFC822和W3CDateTime的格式字符串)  
3. public class DateParser

1. //下载参数   成员下载路径 、在状态栏的标题、保存文件名等等  
2. public class GalDownloadParams

1. //下载任务   使用了同步任务  
2. public class GalDownLoadTask extends  
3.         AsyncTask<GalDownloadParams, Integer, Void>

1. //自定义字符串单元  有UTF编解码、获得sd路径、字符串分割、半角转全角等操作  
2. public class GalStringUtil

1. //自定义url  
2. public class GALURL


1. //输入单元  
2. public class LogUtil

1. //构造MD5值  
2. public class MD5


1. //下载任务监听 加载进程 加载完成 加载失败 加载取消  
2. public class SimpleDownLoadTaskListener implements GalDownLoadTaskListener

1. /*
2.  * 定制自己的HttpClient,确保唯一实例,提供全局访问接口
3.  * 自定义timeout时间等参数
4.  */  
5. public class MyHttpClient

1. //http请求  构造请求参数、请求头、处理返回信息等操作   
2. //使用线程池,来重复利用线程,优化内存  
3. public class GalHttpRequest

1. //线程池的定义  
2. private static ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors  
3.         .newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE);  
4. //Executors的静态方法newCachedThreadPool, newFixedThreadPool, newScheduledThreadPool  
5. //所返回的线程池都是ThreadPoolExecutor对象或者其子类对象.   
6. //ThreadPoolExecutor提供了多种配置, 可以根据实际定制合适的线程池.



测试代码:

1. //初始化单击监听  
2. private void initListener()  
3. {  
4. new OnItemClickListener()  
5.     {  
6. @Override  
7. public void onItemClick(AdapterView<?> parent, View view,  
8. int position, long id)  
9.         {  
10.             String item = (String) parent.getAdapter().getItem(position);  
11. int type = 0;  
12. if ("同步请求InputStream".equalsIgnoreCase(item))  
13.             {  
14.                 type = RequestType.SYNC_REQUESTIS;  
15. else if ("同步请求String".equalsIgnoreCase(item))  
16.             {  
17.                 type = RequestType.SYNC_REQUESTSTRING;  
18. else if ("同步请求Bitmap".equalsIgnoreCase(item))  
19.             {  
20.                 type = RequestType.SYNC_REQUESTBITMAP;  
21. else if ("异步请求InputStream".equalsIgnoreCase(item))  
22.             {  
23.                 type = RequestType.ASYN_REQUESTIS;  
24. else if ("异步请求String".equalsIgnoreCase(item))  
25.             {  
26.                 type = RequestType.ASYN_REQUESTSTRING;  
27. else if ("异步请求Bitmap".equalsIgnoreCase(item))  
28.             {  
29.                 type = RequestType.ASYN_REQUESTBITMAP;  
30. else if ("组装http参数".equalsIgnoreCase(item))  
31.             {  
32.                 type = RequestType.ASYN_EASYPARAMS;  
33. else if ("Post内容".equalsIgnoreCase(item))  
34.             {  
35.                 type = RequestType.ASYN_EASYPOST;  
36.             }  
37. new Intent(GalHttpRequestDemoActivity.this,  
38. class);  
39. "type", type);  
40.             startActivity(intent);  
41.         }  
42.     });  
43. }


1. /**
2.  * @Title: startRequest
3.  * @Description:TODO(这里用一句话描述这个方法的作用)
4.  * @param @param type 传入参数名字
5.  * @return void 返回类型
6.  * @date 2012-4-23 下午11:37:16
7.  * @throw
8.  */  
9. private void startRequest(int type)  
10. {  
11.     GalHttpRequest request;  
12.     textView.setVisibility(View.GONE);  
13.     imageView.setVisibility(View.GONE);  
14. switch (type)  
15.     {  
16. case RequestType.SYNC_REQUESTIS:  
17.     {  
18. // 同步请求InputStream  
19. "同步请求InputStream");  
20. this, PATH_INPUTSTREAM);  
21.   
22. // 如果不检测缓存,则设置:  
23. // request.setCacheEnable(false);  
24. // 必须在调用startXXX()函数之前设置  
25.   
26. // 返回的缓存已经是ufferedInputStream类型  
27.         InputStream is = request.startSynchronous();  
28.         textView.setVisibility(View.VISIBLE);  
29. if (is != null)  
30.         {  
31.             textView.setText(is.toString());  
32.         }  
33. break;  
34.     }  
35. case RequestType.SYNC_REQUESTSTRING:  
36.     {  
37. // 同步请求String  
38. "同步请求String");  
39. this, PATH_STRING);  
40. // 根据服务器返回的状态读取内容,如果服务器内容没有改变,则直接读取缓存内容,如果服务器内容已经修改,则从服务器拉取数据  
41. // 并刷新缓存内容  
42.         String string = request.startSyncRequestString();  
43.         textView.setText(string);  
44.         textView.setVisibility(View.VISIBLE);  
45. break;  
46.     }  
47. case RequestType.SYNC_REQUESTBITMAP:  
48.     {  
49. // 同步请求Bitmap  
50. "同步请求Bitmap");  
51. new BasicHeader("Accept-Language", "zh-cn,zh;q=0.5");  
52. // 支持添加自定义的Http Header请求  
53. this, PATH_BITMAP,  
54. new Header[]  
55.                 { header });  
56. // 请求Bitmap,由于图片基本上不改变,因此如果存在缓存,则直接从缓存读取  
57.         Bitmap bitmap = request.startSyncRequestBitmap();  
58.         imageView.setImageBitmap(bitmap);  
59.         imageView.setVisibility(View.VISIBLE);  
60. break;  
61.     }  
62. case RequestType.ASYN_REQUESTIS:  
63.     {  
64. // 异步请求InputStream  
65. "异步请求InputStream");  
66. this, PATH_INPUTSTREAM);  
67. // 必须先设置回调函数,否则调用异步请求无效  
68. new GalHttpRequestListener()  
69.         {  
70. @Override  
71. public void loadFinished(final InputStream is, boolean fromcache)  
72.             {  
73. // 注意,由于返回的是InputStream,一般情况都需要长时间操作,所以,回调函数是在子线程调用  
74. // 因此使用handler  
75. new Runnable()  
76.                 {  
77. @Override  
78. public void run()  
79.                     {  
80.                         textView.setText(is.toString());  
81.                         textView.setVisibility(View.VISIBLE);  
82.                     }  
83.                 });  
84.             }  
85.   
86. @Override  
87. // 请求失败时,有可能可以从缓存里面读取数据返回  
88. public void loadFailed(final HttpResponse respone,  
89.                     InputStream cacheInputStream)  
90.             {  
91. new Runnable()  
92.                 {  
93.   
94. @Override  
95. public void run()  
96.                     {  
97.                         textView.setText(respone.toString());  
98.                         textView.setVisibility(View.VISIBLE);  
99.                     }  
100.                 });  
101.             }  
102.         });  
103.         request.startAsynchronous();  
104. break;  
105.     }  
106. case RequestType.ASYN_REQUESTSTRING:  
107.     {  
108. "异步请求String");  
109. // 异步请求String  
110. this, PATH_STRING);  
111. // 第一次调用startAsynRequestString或者startAsynRequestBitmap必须在主线程调用  
112. // 因为只有在主线程中调用才可以初始化GalHttprequest内部的全局句柄Handler  
113. new GalHttpLoadTextCallBack()  
114.         {  
115. @Override  
116. public void textLoaded(String text)  
117.             {  
118. // 该部分允许于UI线程  
119.                 textView.setText(text);  
120.                 textView.setVisibility(View.VISIBLE);  
121.             }  
122.         });  
123. break;  
124.     }  
125. case RequestType.ASYN_REQUESTBITMAP:  
126.     {  
127. "异步请求Bitmap");  
128. // 异步请求Bitmap  
129. this, PATH_BITMAP);  
130. new GalHttpLoadImageCallBack()  
131.         {  
132. @Override  
133. public void imageLoaded(Bitmap bitmap)  
134.             {  
135.                 imageView.setImageBitmap(bitmap);  
136.                 imageView.setVisibility(View.VISIBLE);  
137.             }  
138.         });  
139. break;  
140.     }  
141. case RequestType.ASYN_EASYPARAMS:  
142.     {  
143. // 异步组装参数  
144. "组装http参数");  
145. // 交给GalHttprequest自动组装url中的参数  
146. new BasicNameValuePair("p", "51");  
147. this, PATH_WITHPARAMS,  
148.                 feedPair);  
149. new GalHttpLoadTextCallBack()  
150.         {  
151. @Override  
152. public void textLoaded(String text)  
153.             {  
154. // 该部分允许于UI线程  
155.                 textView.setText(text);  
156.                 textView.setVisibility(View.VISIBLE);  
157.             }  
158.         });  
159. break;  
160.     }  
161. case RequestType.ASYN_EASYPOST:  
162.     {  
163. // 异步post 数据给服务器  
164. "异步post 数据给服务器");  
165. // 交给GalHttprequest自动组装url中的参数  
166. this, PATH_POSTCONTENT);  
167. // 设置post内容  
168. "name", "qiuscut");  
169. new GalHttpLoadTextCallBack()  
170.         {  
171. @Override  
172. public void textLoaded(String text)  
173.             {  
174. // 该部分允许于UI线程  
175. "在这里post应该是无效的,因为当前url不支持post");  
176.                 textView.setVisibility(View.VISIBLE);  
177.             }  
178.         });  
179. break;  
180.     }  
181. default:  
182.         finish();  
183. return;  
184.     }  
185. }



学习的目标是成熟!~~~~~