智能的HTTP框架Java類庫:LiteHttp
簡介
LiteHttp是一款簡單、智能、靈活的HTTP框架庫,它在請求和響應層面做到了全自動構建和解析,主要用于Android快速開發。借助LiteHttp你只需要一行代碼即可完美實現http連接,它全面支持GET, POST, PUT, DELETE, HEAD, TRACE, OPTIONS 和 PATCH八種基本類型。LiteHttp能將Java Model轉化為http請求參數,也能將響應的json語句智能轉化為Java Model,這種全自動解析策略將節省你大量的構建請求、解析響應的時間。并且,你能自己繼承重新實現Dataparser這個抽象類并設置給Request,來將http原始的inputstream轉化為任何你想要的東西。
引言
http://litesuits.github.io/guide/http/get-start.htmlLiteHttp引言,一個案例告訴你它的強大之處。
功能及特點
- 單線程,所有方法都基于一個線程,絕不會跨線程,多線程的事情交給它自帶的AsyncExecutor 或者更專業的框架庫來解決。
- 靈活的架構,你可以輕松的替換Json自動化庫、參數構建方式甚至默認的apache http client連接方式。
- 輕量級,微小的的開銷,core jar包僅約86kb。
- 多種請求類型全面支持:get, post, head, put, delete, trace, options, patch.
- 多文件上傳,不需要額外的類庫支持。
- 內置的Dataparser支持文件和位圖下載,你也可以自由的擴展DataParser來把原始的http inputstream轉化為你想要的東西。
- 基于json的全自動對象轉化: 框架幫你完成Java Object Model 和 Http Parameter之間的轉化,完成Http Response與Java Object Model的轉化。
- 自動重定向,基于一定的次數,不會造成死循環。
- 自動gizp壓縮,幫你完成request編碼和response解碼以使http連接更加快速.
- 通過網絡探測完成智能重試 ,對復雜的、信號不良的的移動網絡做特殊的優化。
- 禁用一種或多種網絡, 比如2G,3G。
- 簡明且統一的異常處理體系:清晰、準確的拋出客戶端、網絡、服務器三種異常。
- 內置的AsyncExecutor可以讓你輕松實現異步和并發的http請求,如果你喜歡,隨意使用你自己的AsyncTask或Thread來完成異步,推薦使用更強大、高效的專業并發庫。
架構圖
一個良好的項目結構:
- 底層是業務無關的框架庫,用之四海而皆準。
- 中層是針對業務的三方庫,以及主要邏輯實現,全部業務都在這里。
- 上層是Activity、Fragment、Views&Widget等視圖渲染和業務調用。
這樣一個結構,使得你的代碼快速在phone和pad以及tv之間遷移,且讓你整個更為清晰。那么LiteHttp就位于這個結構的底層。
LiteHttp結構模型:
基本用法
基礎請求
LiteHttpClient client = LiteHttpClient.getInstance(context); Response res = client.execute(new Request("http://baidu.com")); String html = res.getString();
異步請求
HttpAsyncExcutor asyncExcutor = new HttpAsyncExcutor(); asyncExcutor.execute(client, new Request(url), new HttpResponseHandler() { @Override protected void onSuccess(Response res, HttpStatus status, NameValuePair[] headers) { // do some thing on UI thread } @Override protected void onFailure(Response res, HttpException e) { // do some thing on UI thread } });
Java Model 作為參數的請求
// build a request url as : http://a.com?name=jame&id=18 Man man = new Man("jame",18); Response resonse = client.execute(new Request("http://a.com",man));
man class:
public class Man implements HttpParam{ private String name; private int id; private int age; public Man(String name, int id){ this.name = name; this.id= id; } }
全自動Json轉化
String url = "http://litesuits.github.io/mockdata/user?id=18"; User man = client.get(url, null, User.class);
User Class:
public class User extends ApiResult { //全部聲明public是因為寫sample方便,不過這樣性能也好, //即使private變量LiteHttp也能自動賦值,開發者可自行斟酌修飾符。 public UserInfo data; public static class UserInfo { public String name; public int age; public ArrayList<String> girl_friends; } } public abstract class ApiResult { public String api; public String v; public Result result; public static class Result { public int code; public String message; } }
User JSON Structure:
{ "api": "com.xx.get.userinfo", "v": "1.0", "result": { "code": 200, "message": "success" }, "data": { "age": 18, "name": "qingtianzhu", "girl_friends": [ "xiaoli", "fengjie", "lucy" ] } }
多文件上傳
String url = "http://192.168.2.108:8080/LiteHttpServer/ReceiveFile"; FileInputStream fis = new FileInputStream(new File("sdcard/1.jpg")); Request req = new Request(url); req.setMethod(HttpMethod.Post) .setParamModel(new BaiDuSearch()) .addParam("lite", new File("sdcard/lite.jpg"), "image/jpeg") .addParam("feiq", new File("sdcard/feiq.exe"), "application/octet-stream"); if (fis != null) req.addParam("meinv", fis, "sm.jpg", "image/jpeg"); Response res = client.execute(req);
文件和位圖下載
// one way File file = client.execute(imageUrl, new FileParser("sdcard/lite.jpg"), HttpMethod.Get); // other way Response res = client.execute(new Request(imageUrl).setDataParser(new BitmapParser())); Bitmap bitmap = res.getBitmap();
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!