LiteHttp:一款‘智能’的HTTP框架类库



LiteHttp 引言

我们来看一个普通android网络应用上,最基础的http连接和json解析方式,一个简单例子,从服务器获取id=168的用户的信息,User的json结构:

 
 
  1. {
  2. "api": "com.xx.get.userinfo",
  3. "v": "1.0",
  4. "result": {
  5. "code": 200,
  6. "message": "success"
  7. },
  8. "data": {
  9. "age": 18,
  10. "name": "qingtianzhu",
  11. "girl_friends": [
  12. "xiaoli",
  13. "fengjie",
  14. "lucy"
  15. ]
  16. }
  17. }

api, v, result为基础信息,完成这个任务一般分三步:

1. 组织api的url地址和参数(这个很简单),比如:

 
 
  1. String url = "http://litesuits.github.io/mockdata/user?userid=168";

2. http连接网络获取api信息(这个通过封装后还算简单),典型代码:

 
 
  1. /**
  2. * 关闭流顺序:先打开的后关闭;被依赖的后关闭。
  3. * @return string info
  4. */
  5. public static String sendHttpRequst(String apiUrl) {
  6. HttpURLConnection conn = null;
  7. InputStream is = null;
  8. ByteArrayOutputStream baos = null;
  9. try {
  10. URL url = new URL(apiUrl);
  11. conn = (HttpURLConnection) url.openConnection();
  12. conn.connect();
  13. is = conn.getInputStream();
  14. int len = conn.getContentLength();
  15. if (len < 1) len = 1024;
  16. baos = new ByteArrayOutputStream(len);
  17. byte[] buffer = new byte[1024];
  18. len = 0;
  19. while ((len = is.read(buffer)) != -1) {
  20. baos.write(buffer, 0, len);
  21. }
  22. return baos.toString();
  23. } catch (MalformedURLException e) {
  24. e.printStackTrace();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. } finally {
  28. try {
  29. if (baos != null) baos.close();
  30. if (is != null) is.close();
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. if (conn != null) conn.disconnect();
  35. }
  36. return null;
  37. }

3. 解析服务器反馈的string(这是很头疼的):

 
 
  1. public static User parseJsonToUser(String json) {
  2. User user = new User();
  3. if (json != null) {
  4. try {
  5. JSONObject jsonObj = new JSONObject(json);
  6. JSONObject result = jsonObj.optJSONObject("result");
  7. JSONObject data = jsonObj.optJSONObject("data");
  8. JSONArray arr = null;
  9. if (data != null) arr = data.optJSONArray("girl_friends");
  10. user.result = new User.Result();
  11. user.data = new User.UserInfo();
  12. user.data.girl_friends = new ArrayList<String>();
  13. if (jsonObj != null) {
  14. user.api = jsonObj.optString("api");
  15. user.v = jsonObj.optString("v");
  16. }
  17. if (result != null) {
  18. user.result.code = result.optInt("code");
  19. user.result.message = result.optString("message");
  20. }
  21. if (data != null) {
  22. user.data.age = data.optInt("age");
  23. user.data.name = data.optString("name");
  24. }
  25. if (arr != null) {
  26. for (int i = 0, size = arr.length(); i < size; i++) {
  27. user.data.girl_friends.add(arr.optString(i));
  28. }
  29. }
  30. } catch (JSONException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. return user;
  35. }

那么我们看到第3步在User这个类仅仅有3个属性的情况下,就写了约40行代码,如果User有几十个属性,应用中又有几十个类型User的Model,那么代码量将会指数级暴增,这种方式相对会耗费较大的劳动力。

那么,就此转入正题,看看LiteHttp是怎么完成这个任务的:

 
 
  1. User user = client.get(url, null, User.class);

Github地址:https://github.com/litesuits/android-lite-http

项目中有20几个使用案例(Samples),应该足够你起步啦!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值