前言

Android数据层就是数据模型,如果只是随便解析,返回到视图层,那只会代码繁重、维护难、扩展性差,所以一个好解析器设计,会减少开发成本和维护。

前面有讲过一个网络申请回来会回传给一个AsyncHttpResponseHandler,成功和失败会有方法,代码如下:

public class BaseHttpHandler extends AsyncHttpResponseHandler {private HttpCallback response;public int messageID1;public int messageID2;public BaseHttpHandler(HttpCallback response) {this.response = response;}@Overridepublic void onFinish() {super.onFinish();}@Overridepublic void onSuccess(HttpEntity content, int reqType) {super.onSuccess(content, reqType);if (response!= null) {try {Log.i("HU","handle==content="+content);BaseEntity entity=JsonPaserFactory.paserObj(content, reqType);response.onSuccess(EntityUtils.toString(content), entity, reqType);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}@Overridepublic void onFailure(Throwable error, String content, int reqType) {super.onFailure(error, content, reqType);if (response != null) {response.onFailure(error, content, reqType);}}}
大家有注意这行代码:

BaseEntity entity=JsonPaserFactory.paserObj(content, reqType); 此处就是统一解析器,把解析好的数据模型,通过下面这行代码返回业务层:

<span style="white-space:pre"></span>response.onSuccess(EntityUtils.toString(content), entity, reqType);
大家看到两个类 JsonPaserFactory和BaseEntity,JsonPaserFactory工厂解析类,当某一个功能完成后,回来会传给一个类型,然后JsonPaserFactory会返回好数据模型,代码如下:

public class JsonPaserFactory {public static BaseEntity paserObj(String s, int type) {if (s == null || s.trim().toString().equals(""))return null;<span style="color:#ff0000;">BaseEntity entity = (BaseEntity) BaseApplication.httpRes.getPaser(type);</span>try {<span style="color:#ff0000;">entity.paser(s);</span>} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return entity;}public static BaseEntity paserObj(HttpEntity en, int type) {BaseEntity entity = null;if (en != null) {try {entity = paserObj(EntityUtils.toString(en), type);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return entity;}}

首先解析数据类要有个基础类,所有数据类都应该继承父类,就跟设计模式里面工厂模式一样,用户不需要知道每一个具体操作,外面统一会指向每个模块。

/** * 解析实体类 基类 * @author apple_hsp * */public abstract class BaseEntity {public final static String KEY_RESULT="result";public final static String KEY_LIST="list";public final static String KEY_DATE="date";public final static String KEY_NAME="name";public final static String KEY_DESC="desc";public final static String KEY_DATA="data";public  abstract void paser(String data) throws Exception ;public String err;public static  void paserKeys(JSONArray arr,LinkedHashMap<String, String> map)throws Exception{JSONArray keys=arr.getJSONArray(0);JSONArray values=arr.getJSONArray(1);int size =keys.length();for(int i=0;i<size;i++){map.put(keys.getString(i), values.getString(i));}}}

更多相关文章

  1. 白话开发——Android Studio代码调试技巧篇
  2. Windows XP下git通过代理下载android代码
  3. Android的数据存储(二)——SQLite数据库
  4. android数据储存之应用安装位置
  5. Android — Room 数据库跳跃式升级(Migration)
  6. Ubuntu 12.04中下载android源代码

随机推荐

  1. 前端基础,表单、表格作业
  2. 四.Python循环
  3. HTML学习(二) 表单的制作习作
  4. 弹性盒flex的使用
  5. 万能码并不单一(安全专业委员会)
  6. 记录对云服务器的初体验
  7. 创建简历表格和注册表单
  8. 使用css浮动完成网页顶部导航
  9. 基本布局和块状元素
  10. 自定义导航条样式