Android网络层与数据层设计

前言

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

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

public class BaseHttpHandler extends AsyncHttpResponseHandler {

	private HttpCallback response;
	public int messageID1;
	public int messageID2;

	public BaseHttpHandler(HttpCallback response) {
		this.response = response;
	}

	@Override
	public void onFinish() {
		super.onFinish();
	}

	@Override
	public 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 block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public 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 block
			e.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 block
				e.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));
		}
	}
}

你可能感兴趣的:(android,Android架构)