网络请求是大多数应用不可获取的一部分,曾经和一个其他公司的团队一起开发,他们网络请求就是不断的写handler,代码分到了各个地方,我根本读不通
网络请求本身很简单的,写过js的都知道jquery中的ajax都很简单
不吐槽了我们开始
DhNet net=new DhNet("路劲");
net.addParam("key", "参数")
.addParam("key2", "参数2");
.doGet(new NetTask(this) {
@Override
public void doInUI(Response response, Integer transfer) {
//处理返回结果
}
});
上面是最常用的使用方法
创建一个dhnet对象 dhnet对象大多数方法是支持链式操作;
添加参数addParam的值可以为任意对象,如果非空会调用toString方法,
如果参数为为textview或其子类或调用方法getText().toString();
get或method
可以调用
net.setMethod("get或post")
或者
net.doGet(new NetTask(this){xxx});
net.doPost(new NetTask(this){xxx})
关于对话框
net.doGetInDialog(new NetTask(this) {xxx});//get同时打开一个对话框
net.doPostInDialog(new NetTask(this) {xxx});//post同时打开一个对话框
net.doGet(true, new NetTask(this) {xxx});//第一个参数是否显示对话框
对话框中的文字默认get时为 加载中...,post是为提交中...
如果想重写
net.setDialogerMsg("对话框信息");
下面看NetTask回调类
主要的几个方法
//当发生错误时回调这个回调是在UI线程中
public void onErray(Response response)
//返回数据后会现在后台处理
public void doInBackground(Response response1)
//最后会在ui线程处理
public void doInUI(Response response, Integer transfer)
有事在doInBackground处理过程中就需要处理UI这时可以
//在doInBackground添加代码
public void doInBackground(Response response) {
super.doInBackground(response);
//在response中添加传递的对象
response.addBundle("key", "对象传递");
//传递到UI,第二个参数为自定义编码
transfer(response, 100);
}
//在doInUI中
public void doInUI(Response response, Integer transfer) {
if(transfer==100){
//获取传递的对象
Object obj=response.getBundle("key")
}
}
下面来看response对象
response对象指返回的数据
response方法很多
String res=response.plain()//可以获取网络访问的纯文本
当然我们基本网络访问都是json
JSONObject jo=response.jSON()//可以返回网络访问的json对象
网络默认最佳返回格式,当然不是必须的
{
success:true,
msg:'消息内容',
code:'error'
data:{}
}
对应的配置
下面是Const中的配置你可以进行覆盖
public class Const{
public static String response_success = "success";//成功与否
public static String response_msg = "msg";//消息文本
public static String response_data = "data";//数据对象
public static String response_total = "total";//暂时没用
public static String response_current = "current";//暂时没用
public static String response_code = "code";//错误code
}
有了这些后
//假设返回的结果
{success:true,
data:{
user:{
id:1212,
name:'tengzhinei'
}
},
articles:[
{id:1},
{id:2}
]
}
//json处理
boolean success=response.isSuccess();//是否成功,
JSONObject jo = response.jSONFromData();//获取data节点下的对象
JSONObject data = response.jSONFrom("data");;//获取data节点下的对象
JSONObject user = response.jSONFrom("data.user");//获取data.user节点下的对象
JSONArray array = response.jSONArrayFrom("data.articles");//获取data.articles节点下的数组
//也可以直接将json转为bean
User user = response.modelFrom("data.user");//可以直接获取到user对象(这边使用了泛型)
List<Article> articles = response.listFrom(User.class, "data.articles")//获取对象数组
response 很强大吧
这里在顺便说一下ViewUtil 和JSONUtil
下面是我喜欢使用的数据绑定方法
ViewUtil.bindView(findViewById(R.id.time), JSONUtil.getLong(jo, "time"),"time");
//第一个参数view 可以为textview或其子类 ,imageView//如果是imageview 如果第二个参数是字符串会自动下载图片,如果是drawable,bitmap,int或获取直接绑定图片
//第二个数 数据
//第三个数 数据修饰 //这里将数据修饰为时间类型,具体修饰问题在后面讲adapter再讲吧
继续,说过dhnet自带强大的网络缓存下面就说怎么使用吧
DhNet net=new DhNet();
net.setUrl("http://youxianpei.c.myduohuo.com/mobile_index_adbjsonview?id=63&temp=cache_net_error");
//看这里使用缓存策略
net.useCache(CachePolicy.POLICY_ON_NET_ERROR);
net.doGet(new NetTask(getActivity()) {
@Override
public void doInUI(Response response, Integer transfer) {
resultV.setText(response.plain());
//response有方法判断这次结果是不是缓存
if (!response.isCache()) {
dialoger.showToastShort(getActivity(), "这次访问不是使用的缓存,断开网络试试看");
}else{
dialoger.showToastShort(getActivity(), "网络访问失败,这次使用的是缓存");
}
}
});
net.useCache(CachePolicy.XXX);//就可以添加缓存策略了
现在有的缓存策略
//不使用缓存
POLICY_NOCACHE;
//只使用缓存
POLICY_CACHE_ONLY;
//先使用缓存,然后获取但是获取的数据只会缓存起来
POLICY_CACHE_AndRefresh;
//当网络不好时,才会使用缓存
POLICY_ON_NET_ERROR;
//先使用缓存,然后获取,获取后会再使用
POLICY_BEFORE_AND_AFTER_NET;
这些缓存策略都好理解,唯一需要说的POLICY_BEFORE_AND_AFTER_NET
其他策略的doInBackground和doInUI只会被回调一次
但是POLICY_BEFORE_AND_AFTER_NET
doInBackground和doInUI 可能 会被回调两次,一次是上次缓存(如果上次没缓存当然不会回调),一次真正的网络获取的数据
//网络缓存简单吧
在告诉大家一个秘籍
Const.net_error_try=true;
如果这个值被设为true,在doInBackground和doInUI中发生的错误都会被try-catch掉主要是项目上线后,
项目开发过程中还是false 吧
下面文件上传
DhNet net=new DhNet("路径");
net.addParam("key1", "参数1")
.addParam("key2", "参数2").upload("文件参数名", file, 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");
}
}
}
});
//上面的代码就是文件上传了,主要就是使用upload方法
在doInUI会被多次回调用来处理进度,response中有uploading,length,total三个参数用来判断进度
最后文件下载
//文件下载需要注入
@Inject
DownLoadManager downloader;
//启动下载
downloader.download("任务编码","路劲",params,"本地保存路径");//第三个参数附加参数
//注册下载监听
downloader.regeisterCallBack("任务编码",downLoadCallBack);
//取消下载监听
downloader.unregeisterCallBack("任务编码");
可以在activity的onResume监听,onStop中取消监听 特别是一些需要在listview 中显示下载进度的
关于同步网络访问,这些不说
NetUtil.sync(url, method, params);
NetUtil.syncStream(url, method, params);
再底层
HttpManager.execute(HttpGet);
Dhnet 网络就这些东西,基本涉及到网络的方方面面