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的发布让网络请求也变得十分便利,但是我们通常懒得很想用一两句代码实现一 ...
随机推荐
- WEBapi在IIS发布注意事项-发布错误
发布报错:403.14-Forbidden Web 服务器被配置为不列出此目录的内容 解决方法: 1)打开IIS管理器 2)找到功能视图的目录浏览 3)双击进入后,点击右侧操作栏-启用
- hdu4338 Simple Path
Everybody knows that totalfrank has absolutely no sense of direction. Getting lost in the university ...
- js验证后台传递的map数据是否为空
if(JSON.stringify(data)=='{}'){ $("#year").append("<option>--请选择--</option&g ...
- 网站基于vs,复选框,单选款
前端代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.a ...
- mino 路径格式的bucket 数据访问
实施上这个功能很简答,如果官方不支持,我们可以通过基于nginx 的url rewrite 也可以实现 格式说明 如果配置了domain minio 会将 http://mydomain.com/bu ...
- css获取样式
1)类似id.style.width:只能获取<div style="width:200px;">内联样式</div>里面style里的width,即内联样 ...
- Hi3516CV300 sample -> region
- 利用 groupby apply list 分组合并字符
利用 groupby apply list 分组合并字符 因为需要对数据进行分组和合并字符,找到了以下方法. 有点类似 SQL 的 Group BY. import pandas as pd impo ...
- openstack--6--控制节点和计算节点安装配置neutron
Neutron相关介绍 早期的时候是没有neutron,早期所使用的网络的nova-network,经过版本改变才有个neutron. quantum是因为商标和别的公司重名了,又改成的Neutron ...
- mydumper安装、原理介绍
一.安装 安装依赖包: yum install glib2-devel mysql-devel zlib-devel pcre-devel openssl-devel cmake 下载二进制包: ...