Volley.jar下载

在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封装的更多相关文章

  1. 利用Volley封装好的图片缓存处理加载图片

    Volley 工具箱中提供了一种通过 DiskBasedCache 类实现的标准缓存.这个类能够缓存文件到磁盘的指定目录.但是为了使用 ImageLoader,我们应该提供一个自定义的内存 LRC b ...

  2. Volley源码分析(五)Volley源码总结篇

    volley关键的代码这里已经分析完了,下面梳理一下完整的Volley流程 Volley的使用从构造Request对象开始,Volley默认提供了四种request的实现,StringRequest, ...

  3. Rx-Volley 自己来封装

    自从15年接触了RxJava,对函数式编程越发的喜爱.以前Android项目上网络层都是统一的使用Volley,已经对网络请求的回调,多个回调嵌入各种不爽了,趁着年前任务轻松,赶紧的将Volley封装 ...

  4. 【Android】《App研发录》总结

    说明 看这本书的时候,总感觉怪怪的. 因为在地铁上看完的,作者书中基本都是他自己工作中遇到的问题和坑,虽说这样会让人感觉找到了解决方案,可以再进行深入的研究,可是某些地方介绍的有点片面,仅仅是引用部分 ...

  5. Retrofit2 源码解析

    原文链接:http://bxbxbai.github.io/2015/12/13/retrofit2-analysis/ 公司里最近做的项目中网络框架用的就是Retrofit,用的多了以后觉得这个框架 ...

  6. Android 网络请求库volley的封装,让请求更方便

    首先封装一下volley 请求 public class CustomRequest extends StringRequest { private static final String TAG = ...

  7. volley二次封装

    产品中使用Volley框架已有多时,本身已有良好封装的Volley确实给程序开发带来了很多便利与快捷.但随着产品功能的不断增加,服务器接口的不断复杂化,直接使用Volley原生的JSONObjectR ...

  8. Volley自定义Request及使用单例封装RequestQueue

    一.自定义Request Volley的所有的请求的超类型是Resuest,所有我们常用的请求都是这个类的子类,那么我们自定义View肯定也是基于这个类的. 案例: package com.zhy.v ...

  9. 转-封装网络请求库,统一处理通用异常 (基于volley网络请求库)

    http://blog.csdn.net/kroclin/article/details/40540761 一.前言 volley的发布让网络请求也变得十分便利,但是我们通常懒得很想用一两句代码实现一 ...

随机推荐

  1. 【git】git使用

    1.创建github账户 网站:https://github.com/ 注册省略 2.ssk-key客户端配置 作用:不用每次push,clone代码不需要输入用户名+密码 生成ssh-key ssh ...

  2. LeetCode - Most Frequent Subtree Sum

    Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a ...

  3. JS从数组中随机取出几个数组元素的方法

    原文链接:http://caibaojian.com/js-get-random-elements-from-array.html js如何从一个数组中随机取出一个元素或者几个元素. 假如数组为· v ...

  4. Unity 原厂免费资源学习

     

  5. 通过plsql develop查看建表语句

    右键--查看 右下角 如下显示,找出ddl语句 可以看到索引等

  6. 著名软件工程师与作家、极限编程的创始者、JUnit作者之Kent Beck

    Kent Beck,1961年出生,中文名肯特贝克,美国著名软件工程师与作家,在软件工程方面有很大的贡献.他是Smalltalk软件的开发者,设计模式的先驱,测试驱动开发的支持者,也是极限编程的创始者 ...

  7. Cordova+Angularjs+Ionic 混合开发入门讲解

    作为一名学习Android开发的学生,对于移动开发的发展趋势颇为关注,大家都知道,现在原生的移动开发在企业上基本很少使用了,大部分企业为了降低成本,选择了webapp,hybrid(混合开发)这两种模 ...

  8. MySQL5.7(5.6)GTID环境下恢复从库思路方法(转发)

    要讨论如何恢复从库,我们得先来了解如下一些概念: GTID_EXECUTED:它是一组包含已经记录在二进制日志文件中的事务集合 GTID_PURGED:它是一组包含已经从二进制日志删除掉的事务集合. ...

  9. 自定义Write节点的beforerender属性

    由于nuke中的write节点提供了beforerender,afterrender这类事件,我们想添加一些功能只需要在这里面敲入代码即可.事件一旦发生,自然会触发我们敲入的code.   Write ...

  10. HTML和CSS的静态页面

    链接:https://pan.baidu.com/s/1fGajigyQJDLIHqxJn2LXsQ 提取码:7sry 复制这段内容后打开百度网盘手机App,操作更方便哦 设计思路,把网页分成几个模块 ...