新建一个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. myeclipse 添加服务器运行时环境

    像servlet-api.jar.servlet-api.jar服务器能提供的包 解决方法如下: 1,File->New->Other->Server->Server(注意在n ...

  2. 过滤器(Filter)

    day21 过滤器概述 1 什么是过滤器 过滤器JavaWeb三大组件之一,它与Servlet很相似!不它过滤器是用来拦截请求的,而不是处理请求的. 当用户请求某个Servlet时,会先执行部署在这个 ...

  3. BZOJ 1001 狼捉兔子

    Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个 ...

  4. I2C总线之(一)---概述

    概述: I²C 是Inter-Integrated Circuit的缩写,发音为"eye-squared cee" or "eye-two-cee" , 它是一 ...

  5. DEEPIN 2014 正式版 试用体验

    7月6日深度正式发布 Deepin 2014 操作系统,该系统是基于 Ubuntu 14.04 LTS 的 Linux 发行版,并搭载了多款深度开发的软件,更符合国人使用习惯.由于Deepin 201 ...

  6. bzoj1236

    其实这道题目不难,主要要求我们有一个清晰地思路首先可以按位数讨论,这里我把1~9单独讨论了因为除了1位数,每个位数开头的数的开头数字1前面都是-号然后考虑位数的奇偶性当位数为奇数的时候比较简单举个例子 ...

  7. SolrJ总结

    1.solrJ概念 solrJ是Java连接solr进行查询检索和索引更新维护的jar包. 2.项目引入solrJ相关jar包 对于maven工程,直接将下面内容加入到pom文件中即可. <de ...

  8. (转载)PHP json_encode() 函数介绍

    (转载) 在 php 中使用 json_encode() 内置函数(php > 5.2)可以使用得 php 中数据可以与其它语言很好的传递并且使用它. 这个函数的功能是将数值转换成json数据存 ...

  9. C++中的虚函数总结

    一.什么是虚函数.纯虚函数.抽象基类 虚函数:在某基类中声明为 virtual 并在一个或多个派生类中被重新定 义的成员函数. 纯虚函数:是一种特殊的虚函数,使用virtual关键字,并且在其后面加上 ...

  10. poj 2479 Maximum sum (最大字段和的变形)

    题目链接:http://poj.org/problem?id=2479 #include<cstdio> #include<cstring> #include<iostr ...