您的位置:首页 > 其它

dhroid框架介绍之二

2015-11-26 11:41 232 查看
        这篇主要是介绍两点:1.注解方式去声明变量方法赋值。2.网络请求。

        这次没有效果只有源码如下:

public class IocTestActivity extends BaseActivity{
//获取assert中的文本
@InjectAssert(path="testtext.json")
String testassert;
//获取assert中的json
@InjectAssert(path="testtext.json")
JSONObject jo;

//注入文件,因为注入文件时是在新线程里,所以建议在之前的页面就注入一次,不然文件大了会在使用时还没拷贝完成
@InjectAssert(path="anzhi.apk")
File apkFile;
//注入视图
@InjectView(id=R.id.asserttext)
TextView testassertV;
@InjectView(id=R.id.resstring)
TextView resstrV;

//注入视图,和视图事件//itemClick和itemLongClick时间可以参照
//注入时建议向下兼容,如果你的layout中是一个button且不需要换文字,请注入View,这样可以在将布局修改为image时前台不会出错
@InjectView(id=R.id.assertFile,click="toInstal")
View instalApkV;
@InjectView(id = R.id.toFrag, click = "toFrag")
View toFragV;
@InjectView(id=R.id.child_layout)
ViewGroup childLayoutV;
//注入布局文件
@InjectView(layout=R.layout.ioc_head)
View headV;
//在其他视图中查找,查找时是根据别名查找的
@InjectView(id=R.id.intext,inView="headV")
TextView childTextV;
//注入字串
@InjectResource(string=R.string.app_name)
String appname;
//注入颜色
//这里不能为int因为int有默认值0 有值的属性会被忽略,防止重复注入
@InjectResource(color=R.color.link)
Integer colorLink;
//注入图片
@InjectResource(drawable=R.drawable.ic_launcher)
Drawable icDraw;
//注入dimen
@InjectResource(dimen=R.dimen.testdimen)
Integer dime;

//接受传入的字符串
@InjectExtra(name="str",def="默认值")
String extra;
//接受传入的数字
@InjectExtra(name="int",def="1")
Integer extraint;
//接受传入的json对象(传入时是已字符串传入的)这个默认值没用
@InjectExtra(name="jo")
JSONObject extrajo;

//标准注入 单例 注入接口 需要在application中配置
@Inject
IDialog dialoger;

//标准注入 单例 注入类
@Inject
DhDB db;

//根据tag拿对象这里拿到的manager1和manager1copy是同一对象,manager2和manager2copy是同一对象
@Inject(tag="manager1")
TestManager manager1;
@Inject(tag="manager1")
TestManager manager1copy;
@Inject(tag="manager2")
TestManager manager2;
@Inject(tag="manager2")
TestManager manager2copy;

//这个测试根据名字获取对象配置请看application
@Inject(name="testmm")//这里获取到的对象是TestManagerMM
TestManager managermm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在baseActivity 中 调用了 InjectUtil.inject(this); 来注入注解在任意任意类中都可调用

Bitmap output = Bitmap.createBitmap(1000, 1000, Config.ARGB_8888);

setContentView(R.layout.ioc_test_activity);
LayoutParams params=new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
childLayoutV.addView(headV,params);
childTextV.setText("我在注入的布局里");

testassertV.setText("assert text: "+testassert+"assert jo:"+jo);
resstrV.setTextColor(colorLink);
resstrV.setText(appname+" textsize:"+dime);
resstrV.setTextSize(dime);
ViewUtil.bindView(findViewById(R.id.imageView1), icDraw);
ViewUtil.bindView(findViewById(R.id.extras), "extras str:"+extra+" int:"+extraint+" jo:"+extrajo);
manager1.setName("第一个对象");
manager2.setName("第二个对象");
ViewUtil.bindView(findViewById(R.id.inject_stand), "manager1:"+manager1.getName()+" manager1copy:"+manager1copy.getName()+" manager2:"+manager2.getName()+" manager2copy:"+manager2copy.getName());
//通过编码的方式获取对象
TestManager testmanager=IocContainer.getShare().get(TestManager.class, "manager2");
//通过接口 获取单例这个需要在
IDialog d=IocContainer.getShare().get(IDialog.class);
d.showToastShort(this, testmanager.getName());
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// 也可以根据编码获取
// IocContainer.getShare().get("testmm");
dialoger.showToastShort(IocTestActivity.this, managermm.getName());
}
});
findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
TestDateHelper helper=IocContainer.getShare().get(TestDateHelper.class);
dialoger.showToastShort(IocTestActivity.this, helper.getName());
}
});

}
/**
* 视图事件,安装事件
* @param v
*/
public void toInstal(View v) {
if(apkFile==null){
dialoger.showToastLong(this, "文件拷贝中..");
return;
}
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + apkFile.getAbsolutePath()),"application/vnd.android.package-archive");
startActivity(i);
}
public void toFrag(){
Intent it=new Intent(this,MyFragmentActivity.class);
startActivity(it);
}
}上面的东西一目了然,View的初始化赋值,取资源文件里的数据都可以通过注解的方式去声明,炒鸡方便,还有事件的注解都有,ViewUtil.bindView(findViewById(R.id.imageView1), icDraw);这样子一句话就可以将数据绑定到了这个View里面,将解析的数据赋值其实啥都不用说看着代码大家也都明白了。
网络请求如下,就写两个典型的: 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());
}
}
});
}

/**
*
*/
public void onUpload() {
Ioc.get(IDialog.class).showToastShort(getActivity(), "支持多文件长传");
DhNet net = new DhNet("http://www.duohuo.net");
net.addParam("key1", "参数1").addParam("key2", "参数1")
.addFile("key1", apkFile).addFile("key2", apkFile)
.addFile("key3", apkFile).upload(new NetTask(this) {
@Override
public void doInUI(Response response, Integer transfer) {
if (response.isSuccess()) {
Boolean uploading = response.getBundle("uploading");
if (!uploading) {
// 上传完成
} else {
// 已上传大小
long length = response.getBundle("length");
// 文件总大小
long total = response.getBundle("total");
}
}
}
});
}
最下面是多个大文件上传,主要是有两个状态值回调一个是上传完成,还有一个是上传多少数据的状态值,在这个里面可以绘制你的进度条你只需要专注你的绘制就可以了,传入你需要上传的文件即可,是不是很方便,上面那个方法则是数据的请求解析一句话就可以了,无论你是想获得数组对象还是获取某个实体对象又或者你只想获取某个字段的值都只需要一句话搞定,都很方便。其实要说的细的话还有很多可以说的,但是我只是挑了重点和常用的来说,毕竟大家都是程序员,其他的对照看看也就理解了。


<
94f6
/p>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dhroid