[关闭]
@liter 2015-10-23T14:51:37.000000Z 字数 2375 阅读 6700

LiteHttp 引言:开发者为什么可以选LiteHttp网络框架?

litehttp2.x版本系列教程


官网: http://litesuits.com

QQ群: 大群 47357508二群 42960650

本系列文章目录总览: https://zybuluo.com/liter/note/186513


开发者为什么要选用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,那么代码量将会指数级暴增,这种方式相对会耗费较大的劳动力。

那么,就此转入正题

看看lite-http是怎么完成这个任务的:

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

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

项目中有20+个使用案例(Samples),带你飞。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注