Volley封装
在Application初始化 Volley queues=Volley.newRequestQueue(appContext);
并返回RequestQueue 对象
public static RequestQueue getHttpQueues(){
return queues;
}
import android.content.Context; import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
importpublic abstract class VolleyInterface {
public Context context;
public static Listener<ResultModel> mListener;
public static ErrorListener mErrorListener; public VolleyInterface(Context context,Listener<ResultModel> lisener,ErrorListener errorLisener){
this.context=context;
mListener=lisener;
mErrorListener=errorLisener;
} public Listener<ResultModel> loadingListener(){
mListener=new Listener<ResultModel>() { @Override
public void onResponse(ResultModel resultModel) {
onMySuccess(resultModel);
}
};
return mListener;
} public ErrorListener errorListener(){
mErrorListener=new ErrorListener() { @Override
public void onErrorResponse(VolleyError volleyError) {
OnMyError(volleyError);
}
};
return mErrorListener;
} public abstract void onMySuccess(ResultModel resultModel);
public abstract void OnMyError(VolleyError volleyError); }
VolleyRequest类
import java.util.Map; import android.content.Context; import com.android.volley.Request.Method;
importimportpublic class VolleyRequest {
private static FastJsonRequest<ResultModel> fastJsonRequest;
public static Context context;
public static void RequestGet(Context mContext,String url,String tag,VolleyInterface vif){
FandDaApplication.getHttpQueues().cancelAll(tag);
fastJsonRequest=new FastJsonRequest<ResultModel>(url, ResultModel.class, vif.loadingListener(), vif.errorListener());
fastJsonRequest.setTag(tag);
FandDaApplication.getHttpQueues().add(fastJsonRequest);
//调用会引发com.android.volley.NoConnectionError: java.io.InterruptedIOException错误,原因是volley已经调用过了
//FandDaApplication.getHttpQueues().start();
} public static void RequestPost(Context context,String url,Map<String,String> map,String tag,VolleyInterface vif){
FandDaApplication.getHttpQueues().cancelAll(tag);
fastJsonRequest=new FastJsonRequest<ResultModel>(Method.POST,url, ResultModel.class, map,vif.loadingListener(), vif.errorListener());
fastJsonRequest.setTag(tag);
FandDaApplication.getHttpQueues().add(fastJsonRequest);
//调用会引发com.android.volley.NoConnectionError: java.io.InterruptedIOException错误,原因是volley已经调用过了
//FandDaApplication.getHttpQueues().start();
} }
使用方式
String url=new BroadcastAPI().getBroadcastList(0,"",0,"",page,pageSize);
VolleyRequest.RequestGet(context, url, "NEWINVILIST",
new VolleyInterface(context,VolleyInterface.mListener,VolleyInterface.mErrorListener) {
@Override
public void onMySuccess(ResultModel resultModel) {
if(ResultApi.isCode(resultModel)){
list =JSON.parseArray(resultModel.getResult().toString(), BroadcastDetailsModel.class);
//如果用户刷新数据则清空原来缓存记录,为了保证数据统一性
BroadcastTable.getInstance(context).deleteAllData(BroadcastTable.newNumber);
if (mType == 1) {
adapter.addItemTop(list);
} else {
adapter.addItemLast(list);
}
BroadcastTable.getInstance(context).saveBroadcastList(list,BroadcastTable.newNumber);
}else{
if(mType==1){
ToastUtil.show(context, getString(R.string.toast_empty_data));
}else{
ToastUtil.show(context, getString(R.string.toast_next_empty));
}
}
mSwipeRefreshWidget.setRefreshing(false);
} @Override
public void OnMyError(VolleyError volleyError) {
ToastUtil.show(context, volleyError.getMessage());
mSwipeRefreshWidget.setRefreshing(false);
}
});
public class ResultModel {
private int code;
private String msg;
private Object result;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
Volley封装的更多相关文章
- 利用Volley封装好的图片缓存处理加载图片
Volley 工具箱中提供了一种通过 DiskBasedCache 类实现的标准缓存.这个类能够缓存文件到磁盘的指定目录.但是为了使用 ImageLoader,我们应该提供一个自定义的内存 LRC b ...
- Volley源码分析(五)Volley源码总结篇
volley关键的代码这里已经分析完了,下面梳理一下完整的Volley流程 Volley的使用从构造Request对象开始,Volley默认提供了四种request的实现,StringRequest, ...
- Rx-Volley 自己来封装
自从15年接触了RxJava,对函数式编程越发的喜爱.以前Android项目上网络层都是统一的使用Volley,已经对网络请求的回调,多个回调嵌入各种不爽了,趁着年前任务轻松,赶紧的将Volley封装 ...
- 【Android】《App研发录》总结
说明 看这本书的时候,总感觉怪怪的. 因为在地铁上看完的,作者书中基本都是他自己工作中遇到的问题和坑,虽说这样会让人感觉找到了解决方案,可以再进行深入的研究,可是某些地方介绍的有点片面,仅仅是引用部分 ...
- Retrofit2 源码解析
原文链接:http://bxbxbai.github.io/2015/12/13/retrofit2-analysis/ 公司里最近做的项目中网络框架用的就是Retrofit,用的多了以后觉得这个框架 ...
- Android 网络请求库volley的封装,让请求更方便
首先封装一下volley 请求 public class CustomRequest extends StringRequest { private static final String TAG = ...
- volley二次封装
产品中使用Volley框架已有多时,本身已有良好封装的Volley确实给程序开发带来了很多便利与快捷.但随着产品功能的不断增加,服务器接口的不断复杂化,直接使用Volley原生的JSONObjectR ...
- Volley自定义Request及使用单例封装RequestQueue
一.自定义Request Volley的所有的请求的超类型是Resuest,所有我们常用的请求都是这个类的子类,那么我们自定义View肯定也是基于这个类的. 案例: package com.zhy.v ...
- 转-封装网络请求库,统一处理通用异常 (基于volley网络请求库)
http://blog.csdn.net/kroclin/article/details/40540761 一.前言 volley的发布让网络请求也变得十分便利,但是我们通常懒得很想用一两句代码实现一 ...
随机推荐
- Linux命令对应英文全称
https://blog.csdn.net/yrc_note/article/details/72598780 拓展:https://blog.csdn.net/u010613363/article/ ...
- python列表复习
列表的切片 >>> name = list()>>> name.extend(range(100))>>> name[0, 1, 2, 3, 4, ...
- struts2(三)拦截器
拦截器 需求 如果要访问某一个action类中的某一个方法的内容,如果是admin用户,则访问,如果不是admin用户,则不能访问. 实现 缺点 权限判断的代码和业务逻辑代码混合在一起了 利用拦截器 ...
- url和资源的再理解
元数据管理系统中, 确实是所有的静态资源都放在WebContent 不在dgs这个主项目中,通过url访问了 下面的这个项目在dgs中
- MySQL Network--Localhost与127.0.0.1的差异
localhost为本地服务器,而127.0.01为本机地址.在使用localhost时不经过网卡传输,不受网络防火墙和网卡相关的限制,访问localhost不会被解析成ip地址,不会占用网卡和网络资 ...
- Using pointer to access array instead of index
See example below firstly. uint8_t parity = ; uint8_t index = ; //flag gMUXTask.responseData[index++ ...
- 05机器学习实战之Logistic 回归scikit-learn实现
https://blog.csdn.net/zengxiantao1994/article/details/72787849似然函数 原理:极大似然估计是建立在极大似然原理的基础上的一个统计方法,是概 ...
- Linux或树莓派3——挂载U盘、移动硬盘并设置rwx权限
话说最近在树莓派上搭建了一个owncloud,因为树莓派的存储空间有限,就插了个16G的U盘,然后设置成开机自动挂载.这里稍微注意一下的是U盘的格式最好不要NTFS,因为一般情况下NTFS格式的文件系 ...
- MFC message routine
现在维护的一个软件还是用mfc写的,最近被要加入一个功能弄得焦头烂额.主要现象就是加入的菜单的响应函数没被call到 上网搜索,在官方网站找到了不少资料 主要链接如下 https://msdn.mic ...
- [转]xargs详解
为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人写的脚本里面也经常会遇到,但是却很容易与管道搞混淆,本篇会详细讲解到底什么是xargs命令,为什么要用xargs命令以 ...