Volley的简单二次封装
新建一个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的简单二次封装的更多相关文章
- 简单二次封装的Golang图像处理库:图片裁剪
简单二次封装的Golang图像处理库:图片裁剪 一.功能 Go语言下的官方图像处理库 简单封装后对jpg和png图像进行缩放/裁剪的库 二.使用说明 1.首先下载 go get -v -u githu ...
- axios 简单二次封装
import axios from 'axios' import { Message } from 'element-ui'; // 设置baseURL //axios.defaults.baseUR ...
- Volley的简单封装
算了一下,好像有很久没有写博客了.其实,关于写博客这件事,我从来没有把他当成我的一种任务,而是在学习过程中的一种总结和自我发现,同样也是为了练一练文笔,说不定有一天,我也能出一本书像<第一行代码 ...
- volley二次封装
产品中使用Volley框架已有多时,本身已有良好封装的Volley确实给程序开发带来了很多便利与快捷.但随着产品功能的不断增加,服务器接口的不断复杂化,直接使用Volley原生的JSONObjectR ...
- Vue:对axios进行简单的二次封装
主要做3点: 1.配置一个请求地址前缀 2.请求拦截(在请求发出去之前拦截),给所有的请求都带上 token 3.拦截响应,遇到 token 不合法则报错 // 对 axios 的二次封装 impor ...
- OkHttp框架从入门到放弃,解析图片使用Picasso裁剪,二次封装OkHttpUtils,Post提交表单数据
OkHttp框架从入门到放弃,解析图片使用Picasso裁剪,二次封装OkHttpUtils,Post提交表单数据 我们这片博文就来聊聊这个反响很不错的OkHttp了,标题是我恶搞的,本篇将着重详细的 ...
- 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)
前言 首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已. 下面先介绍一下WebUploader 简介: WebUp ...
- 对jquery的ajax进行二次封装以及ajax缓存代理组件:AjaxCache
虽然jquery的较新的api已经很好用了, 但是在实际工作还是有做二次封装的必要,好处有:1,二次封装后的API更加简洁,更符合个人的使用习惯:2,可以对ajax操作做一些统一处理,比如追加随机数或 ...
- pywinauto二次封装(pywinnat.py)
将pywinauto常用方法进行封装,使得pywinauto用起来更简单 #头文件的引入 from pywinauto import application from pywinauto import ...
随机推荐
- BZOJ 3672 购票
Description 今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国\(n\)个城市的OIer们都会从各地出发,到SZ市参加这次盛会. 全国的城市构成了一棵以SZ市为根的有根树,每个城市与 ...
- [BZOJ 3680] 吊打XXX 【模拟退火】
题目链接:BZOJ - 3680 题目分析 这道题是SLYZ的神犇把JSOI的平衡点那道题改了一下题面变成了吊打GTY神犇..Orz 第一次写模拟退火,只能照着别人的代码写,我看的是PoPoQQQ神犇 ...
- [CF Round #294 div2] D. A and B and Interesting Substrings 【Map】
题目链接:D. A and B and Interesting Substrings 题目大意 给定26个小写字母的权值,一共26个整数(有正有负). 给定一个小写字母组成的字符串(长度10^5),求 ...
- 网站性能优化— WebP 全方位介绍
谈到优化网站性能时,主要目标之一就是减少要发送到浏览器的数据量(即 payload).而当前,图片通常是页面构成中最耗费流量的部分,因此降低图片的大小是一个最为有效的优化网页前端性能的办法. 有很多工 ...
- Linux怪哉ntfs
http://www.linuxidc.com/Linux/2013-08/88721.htm
- 20140704笔试面试总结(java)
1.java数组定义 1.与其他高级语言不同,Java在数组声明时并不为数组分配存储空间,因此,在声明的[]中不能指出数组的长度 2.为数组分配空间的两种方法:数组初始化和使用new运算符 3.未分配 ...
- NHibernate 存储过程使用
NHibernate也是能够操作存储过程的,不过第一次配置可能会碰到很多错误. 一.删除 首先,我们新建一个存储过程如下: CREATE PROC DeletePerson @Id int AS DE ...
- HDOJ/HDU 2555 人人都能参加第30届校田径运动会了(判断加排序~)
Problem Description 杭州师范大学第29届田径运动会圆满的闭幕了,本届运动会是我校规模最大,参赛人数最多的一次运动会.在两天半时间里,由学生.教工组成的61支代表队共2664名运动员 ...
- Android Studio下载及离线升级方法
由于众所周知的原因,android官网无法访问,所以我们要用到翻.墙.工具,我用的是自.由.门,大家自行搜索下载. android studio下载地址: https://dl.google.com/ ...
- net user命令
net user net user 用户名 net user 用户名 密码 /add net user 用户名 /del net localgroup administrators net local ...