标签:
dhroid 目前包含了6大组件供大家使用
1.Ioc容器: (用过spring的都知道)视图注入,对象注入,接口注入,解决类依赖关系
2.Eventbus: android平台事件总线框架,独创延时事件,事件管理轻松
3.Dhnet: 网络http请求的解决方案,使用简单,减少代码,自带多种网络访问缓存策略
4.adapter模块: 数据绑定轻松,不用写多余的adapter,天生网络支持(一行代码搞定加载,刷新问题)
5.DhDb: android中sqlite的最轻量orm框架(增删改查轻松搞定)
6.Perference: android自带Perference 升级版,让你的Perference更强大,更方便
说了这么多,来点干货吧
注解应用
//对应方法toTest,对应View是toIocTest,可以在toTest(View v)中做时间处理
@InjectView(id = R.id.ioctest, click = "toTest")View toIocTest;
//无对应方法,可以直接对title_name进行相应的操作 @InjectView(id = R.id.title_name)TextView title_name;
Dhnet应用
get方式,获取到数据后,更新UI
/** * get测试 * * @param key * */ public void onTestGet(View v) { String URL = "http://youxianpei.c.myduohuo.com/mobile_index_adbjsonview?id=63"; DhNet net = new DhNet(URL); // 添加参数 net.addParam("key1", "key1"); net.doGet(new NetTask(this) { @Override public void onErray(Response response) { super.onErray(response); // 错误处理,出错后会先关闭对话框然后调用这个方法,默认不处理 } @Override public void doInBackground(Response response) { super.doInBackground(response); // 后台处理信息 // 可以向UI层传递数据 response.addBundle("keyBundle", "N0"); transfer(response, 100); } @Override public void doInUI(Response response, Integer transfer) { if (transfer == 100) { dialoger.showToastShort(getActivity(), response.getBundle("keyBoundle").toString()); } else { resultV.setText(response.plain()); } } }); }
get方式,获取数据中显示进度条,取得数据后更新UI
/** * get测试同时打开进度 * * @param v */ public void onTestGetDialoge(View v) { DhNet net = new DhNet( "http://youxianpei.c.myduohuo.com/mobile_index_adbjsonview?id=63"); // net.setProgressMsg("进度提示消息"); //对话框样式请在IOC实现IDialog 接口 net.doGet(true,"进度提示消息",new NetTask(this) { @Override public void doInUI(Response response, Integer transfer) { resultV.setText(response.plain()); } }); }
post方式
/** * post测试 * * @param v */ public void onTestPost(View v) { DhNet net = new DhNet( "http://youxianpei.c.myduohuo.com/mobile_index_adbjsonview?id=63"); NetTask task = new NetTask(this) { @Override public void doInUI(Response response, Integer transfer) { resultV.setText(response.plain()); } }; if (v.getId() == R.id.posttest) { net.doPost(task); } else if (v.getId() == R.id.postdialogtest) { net.doPostInDialog(task); } }
解析JSON
public void onTrans(final View v) { DhNet net = new DhNet(); net.setUrl("http://youxianpei.c.myduohuo.com/mobile_index_adbjsonview?id=63&temp=trans"); net.doGet(new NetTask(getActivity()) { @Override public void doInUI(Response response, Integer transfer) { // 结果转JOSN if (v.getId() == R.id.to_json) { JSONArray array = response.jSONArrayFromData(); resultV.setText(array.toString()); // 获取某个节点下的json // JSONObject urlinfo=response.jSONFrom("xxx.xxx"); // dialoger.showToastLong(getActivity(), // urlinfo.toString()); // 这是结果的纯文本 // response.plain(); // 获取某个节点下的jsonarray // response.jSONArrayFrom("xxx.xxx"); // 将整个节点转为json对象 // response.jSON(); } else if (v.getId() == R.id.to_bean) { // List<AdBean> ads = response.listFrom(AdBean.class, "data"); resultV.setText(ads.toString()); // 获取某个节点下的对象 // AdBean bean=response.modelFrom("xxx.xxx"); // dialoger.showToastLong(getActivity(), bean.toString()); } } }); }
Adapter
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.adapter_list_activity); adapter=new NetJSONAdapter("http://xlzx.sinaapp.com/json.php", this, R.layout.adapter_item); adapter.setDataBulider(new DataBulider() { @Override public JSONArray onDate(Response response) { //如果你的结果不是在某个节点而是需要处理后才有的 return response.jSONArrayFrom("employees"); } }); //添加参数 adapter.addParam("key1", "key1"); //数据绑定 adapter.addField("name", R.id.name); adapter.addField("title", R.id.title); //数据绑定 进行文本修饰 adapter.addField("pubdate", R.id.time,DemoValueFixer.time); //数据绑定 进行图片修饰 adapter.addField("user_faceimg", R.id.pic,DemoValueFixer.pic_round); adapter.addField(new FieldMap("activeaddress", R.id.content) { @Override public Object fix(View itemV, Integer po, Object o, Object jo) { JSONObject joo=(JSONObject) jo; //这里可以做一些额外的工作 itemV.findViewById(R.id.icon).setVisibility(JSONUtil.getInt(joo, "status")==1?View.VISIBLE:View.INVISIBLE); return o; } }); //内部点击事件 adapter.setOnInViewClickListener(R.id.pic, new BeanAdapter.InViewClickListener() { @Override public void OnClickListener(View itemV, View v, Integer po, Object jo) { JSONObject joo=(JSONObject) jo; dialoger.showToastLong(getActivity(), JSONUtil.getString(joo, "username")); } }); //加载成功后回调 adapter.setOnLoadSuccess(new LoadSuccessCallBack() { @Override public void callBack(Response response) { if(response.isSuccess()){ dialoger.showToastShort(getActivity(), "加载成功"); if(adapter.getPageNo()==1){ listV.setSelection(0); } } } }); adapter.refresh(); adapter.useCache(CachePolicy.POLICY_CACHE_AndRefresh); adapter.showProgressOnFrist(true); listV.setAdapter(adapter); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.adapter_list_refresh_more); adapter=new NetJSONAdapter("http://xlzx.sinaapp.com/json.php", this, R.layout.adapter_item); adapter.setDataBulider(new DataBulider() { @Override public JSONArray onDate(Response response) { //如果你的结果不是在某个节点而是需要处理后才有的 return response.jSONArrayFrom("employees"); } }); //添加参数 adapter.addParam("key1", "key1"); //数据绑定 adapter.addField("name", R.id.name); adapter.addField("title", R.id.title); //数据绑定 进行文本修饰 adapter.addField("pubdate", R.id.time,DemoValueFixer.time); //数据绑定 进行图片修饰 adapter.addField("user_faceimg", R.id.pic,DemoValueFixer.pic_round); adapter.addField(new FieldMap("activeaddress", R.id.content) { @Override public Object fix(View itemV, Integer po, Object o, Object jo) { JSONObject joo=(JSONObject) jo; //这里可以做一些额外的工作 itemV.findViewById(R.id.icon).setVisibility(JSONUtil.getInt(joo, "status")==1?View.VISIBLE:View.INVISIBLE); return o; } }); //内部点击事件 adapter.setOnInViewClickListener(R.id.pic, new BeanAdapter.InViewClickListener() { @Override public void OnClickListener(View itemV, View v, Integer po, Object jo) { JSONObject joo=(JSONObject) jo; dialoger.showToastLong(getActivity(), JSONUtil.getString(joo, "username")); } }); //加载成功后回调 adapter.setOnLoadSuccess(new LoadSuccessCallBack() { @Override public void callBack(Response response) { if(response.isSuccess()){ dialoger.showToastShort(getActivity(), "加载成功"); if(adapter.getPageNo()==1){ listV.setSelection(0); } } } }); adapter.refresh(); adapter.showProgressOnFrist(true); listV.setAdapter(adapter); }
标签:
原文地址:http://www.cnblogs.com/Jsonlu/p/4746018.html