新建一个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. 机器视觉工具箱-Machine Vision Toolbox for Matlab

    发现了一个机器视觉的Matlab工具箱,分享一下. 机器视觉工具箱(MVT的)规定,在机器视觉和基于视觉的控制有益的多种功能.这是一个有点折衷收集反映作者在光度学,摄影测量,色度学 方面的个人利益.它 ...

  2. 手写js面向对象选项卡插件

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  3. http server v0.1_http_webapp.c

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...

  4. HTML5区域范围文本框实例页面

    CSS代码: input { font-size: 14px; font-weight: bold; } input[type=range]:before { content: attr(min); ...

  5. LeetCode 面试:Add Binary

    1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...

  6. 一起啃PRML - 1 Introduction 绪论

    一起啃PRML - 1 Introduction @copyright 转载请注明出处 http://www.cnblogs.com/chxer/ 这一部分主要是介绍一下Pattern Recogni ...

  7. Idea学习笔记

    (1)安装,启动,配置: 1.安装完的插件如果不需要的插件可以不启动它:可以在setting里面配置plugins勾选不需要的插件:好处是加快idea的启动: 2.安装完成之后启根据系统32位,64位 ...

  8. maven配置默认jdk版本

    在maven的默认配置中,对于jdk的配置是1.4版本,那么创建/导入maven工程过程中,工程中未指定jdk版本. 对工程进行maven的update,就会出现工程依赖的JRE System Lib ...

  9. 数据结构(莫队算法):国家集训队2010 小Z的袜子

    [题目描述] 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命…… 具体来说,小Z把这N只袜子从1到 ...

  10. unicode随笔小计

    科普字符集: ascii:一个字节,占8位,(0000 0000 - 1111 1111) 如果只是英语那就没什么问题. 后来,不同的语言有了编码诞生.为了统一,出现一个大集合.便有了. unicod ...