新建一个application

 package com.honghe.myvolley.app;

 import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley; import android.app.Application; public class MyApplication extends Application {
private static RequestQueue queues; @Override
public void onCreate() {
super.onCreate();
queues = Volley.newRequestQueue(getApplicationContext());
} public static RequestQueue getHttpQueue() { return queues;
} }

在xml文件中注册为启动的application

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.honghe.myvolley"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.INTERNET"/> <application
android:name="com.honghe.myvolley.app.MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="com.honghe.myvolley.Activity.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

编写volleyrequest类,实现get和post方法

 package com.honghe.myvolley.Volley;

 import java.util.Map;

 import android.content.Context;

 import com.android.volley.AuthFailureError;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.StringRequest;
import com.honghe.myvolley.app.MyApplication; public class VolleyRequest {
public static StringRequest stringRequest;
public static Context context; public static void RequestGet(Context mContext, String url, String tag,
VolleyInterface vif) {
MyApplication.getHttpQueue().cancelAll(tag);
stringRequest = new StringRequest(Method.GET, url,
vif.loadingListener(), vif.errorListener());
stringRequest.setTag(tag);
MyApplication.getHttpQueue().add(stringRequest);
MyApplication.getHttpQueue().start();
} public static void RequestPost(Context mContext, String url, String tag,
final Map<String, String> params, VolleyInterface vif) {
MyApplication.getHttpQueue().cancelAll(tag);
stringRequest = new StringRequest(url, vif.loadingListener(),
vif.errorListener()) { @Override
protected Map<String, String> getParams() throws AuthFailureError { return params;
} }; stringRequest.setTag(tag);
MyApplication.getHttpQueue().add(stringRequest);
MyApplication.getHttpQueue().start();
} }

设置volleyInterface的回调

 package com.honghe.myvolley.Volley;

 import android.content.Context;

 import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError; public abstract class VolleyInterface { public Context mContext;
public Listener<String> mListener;
public ErrorListener mErrorListener; public VolleyInterface(Context context) {
this.mContext = context;
} public Listener<String> loadingListener() {
mListener = new Listener<String>() { @Override
public void onResponse(String arg0) {
OnMySuccess(arg0);
}
};
return mListener;
} public ErrorListener errorListener() { mErrorListener = new ErrorListener() { @Override
public void onErrorResponse(VolleyError arg0) {
OnMyError(arg0);
}
};
return mErrorListener;
} public abstract void OnMySuccess(String result); public abstract void OnMyError(VolleyError arg0);
}

如何使用

 protected void VolleyGet(String url, String tag) {
VolleyRequest.RequestGet(this, url, tag, new VolleyInterface(this) { @Override
public void OnMySuccess(String result) { } @Override
public void OnMyError(VolleyError arg0) { }
});
}

Volley的简单二次封装的更多相关文章

  1. 简单二次封装的Golang图像处理库:图片裁剪

    简单二次封装的Golang图像处理库:图片裁剪 一.功能 Go语言下的官方图像处理库 简单封装后对jpg和png图像进行缩放/裁剪的库 二.使用说明 1.首先下载 go get -v -u githu ...

  2. axios 简单二次封装

    import axios from 'axios' import { Message } from 'element-ui'; // 设置baseURL //axios.defaults.baseUR ...

  3. Volley的简单封装

    算了一下,好像有很久没有写博客了.其实,关于写博客这件事,我从来没有把他当成我的一种任务,而是在学习过程中的一种总结和自我发现,同样也是为了练一练文笔,说不定有一天,我也能出一本书像<第一行代码 ...

  4. volley二次封装

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

  5. Vue:对axios进行简单的二次封装

    主要做3点: 1.配置一个请求地址前缀 2.请求拦截(在请求发出去之前拦截),给所有的请求都带上 token 3.拦截响应,遇到 token 不合法则报错 // 对 axios 的二次封装 impor ...

  6. OkHttp框架从入门到放弃,解析图片使用Picasso裁剪,二次封装OkHttpUtils,Post提交表单数据

    OkHttp框架从入门到放弃,解析图片使用Picasso裁剪,二次封装OkHttpUtils,Post提交表单数据 我们这片博文就来聊聊这个反响很不错的OkHttp了,标题是我恶搞的,本篇将着重详细的 ...

  7. 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)

    前言 首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已. 下面先介绍一下WebUploader 简介: WebUp ...

  8. 对jquery的ajax进行二次封装以及ajax缓存代理组件:AjaxCache

    虽然jquery的较新的api已经很好用了, 但是在实际工作还是有做二次封装的必要,好处有:1,二次封装后的API更加简洁,更符合个人的使用习惯:2,可以对ajax操作做一些统一处理,比如追加随机数或 ...

  9. pywinauto二次封装(pywinnat.py)

    将pywinauto常用方法进行封装,使得pywinauto用起来更简单 #头文件的引入 from pywinauto import application from pywinauto import ...

随机推荐

  1. 我学C的那些年[ch02]:宏,结构体,typedef

    c语言的编译过程: 预处理 编译 汇编 链接 而预处理中有三种情况: 文件包含( #include ) 条件编译(#if,#ifndef,#endif) 宏定义( #define ) 宏就是预处理中的 ...

  2. 不支持关键字: “userid”。

    运行程序提示:不支持关键字: “userid”. 找了很久在一篇博客里面提示web.config数据库字符串链接出错.

  3. theano中对图像进行convolution 运算

    (1) 定义计算过程中需要的symbolic expression """ 定义相关的symbolic experssion """ # c ...

  4. [UOJ 74] 【UR #6】破解密码

    题目链接:UOJ - 74 题目分析 题目中,将字符串 S 的第一个字符移到末尾,其他字符向前移动一个位置,f(S) 就从 Hi 变成了 Hi+1. 我们分析一下这个过程:假设第一个字符为 c, (H ...

  5. OTG线与普通USB线的区别

    转自OTG线与普通USB线的区别 USB数据线是我们常见的设备,OTG线作为近年来随着手机行业的快速发展,逐步进入了我们的日常使用范围.OTG线与普通USB线的有什么区别?       USB数据线用 ...

  6. 【HDU4552】 怪盗基德的挑战书(后缀数组)

    怪盗基德的挑战书 Problem Description “在树最美丽的那天,当时间老人再次把大钟平均分开时,我会降临在灯火之城的金字塔前,带走那最珍贵的笑容.”这是怪盗基德盗取巴黎卢浮宫的<蒙 ...

  7. YII model模型和登陆详解

    模型是 CModel 或其子类的实例.模型用于保持数据以及与其相关的业务逻辑. 模型是单独的数据对象.它可以是数据表中的一行,或者一个用户输入的表单. 数据对象的每个字段对应模型中的一个属性.每个属性 ...

  8. bzoj2165

    类似于bzoj1706,考虑到楼层是等价的我们令f[p,i,j]为用了2^p次电梯,从房间i到j最多上升了多少层然后从2^p很容易推到2^(p+1),类似于floyd的转移即可下面我们要用最小的电梯次 ...

  9. 【转】Adnroid4.0 签名混淆打包(conversion to dalvik format failed with error 1)

    原文网址:http://jojol-zhou.iteye.com/blog/1220541 自己的解决方法:关闭Eclipse,再开启Eclipse就可以. 最新Eclipse3.7+android ...

  10. MTD应用学习:mtd和mtdblock的区别

    http://my.oschina.net/shelllife/blog/123482 http://www.cnblogs.com/hnrainll/archive/2011/06/09/20760 ...