整個框架是基於阿帕奇的httpclient搭建的,框架類圖以下,我是用Astah Professional畫出來,將項目導入,自動生成全部類關係圖,仍是挺方便的;java
核心類是HttpManager和HttpAsyncTakeManager,前者基於threa實現,後者基於asyncTask實現,封裝了get和post請求,只須要實例化,而且調用request方法便可;android
- HttpAsyncTaskManager http=new HttpAsyncTaskManager(this);
- String url = "http://www.12306.cn/mormhweb/kyfw/ypcx/";
- http.request(url, new StringTaskHandler() {
- @Override
- public void onNetError() {
-
- System.out.println("---------neterror---------");
- }
-
- @Override
- public void onSuccess(String result) {
-
- System.out.println("#####" + result);
- }
-
- @Override
- public void onFail() {
-
- System.out.println("---------onFail---------");
- }
- });
TaskHandler<T>是一個抽象泛型類,有四個子類,StringTaskHandler,InputStreamTaskHandler,JsonObjectHandler,JsonArrayHandler,分別能夠處理xml流,字符串,json對象;還能夠自定義擴展TaskHandler,實現本身的邏輯git
- public abstract class TaskHandler<T> {
-
- public abstract void onNetError();
-
-
-
-
-
-
- public abstract void onSuccess(T result);
-
-
- public abstract void onFail();
-
-
-
-
-
-
- public abstract T parseResult(InputStream result);
- }
一句代碼就搞定,請求,有木有很方便呢,整個項目我已經放在github上面了https://github.com/allen1988/android_http_framework