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 ...
随机推荐
- 4542: [Hnoi2016]大数
Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小B还有一个素数P.现在,小 B 提出了 M 个询问,每个 ...
- 【Maven】解决maven打jar包报错 source 1.3 中不支持注释 (请使用 -sour
问题:maven在进行打包时,报 '请使用-source 5 或者更高版本以启用XX'的信息并导致打包失败. 原因:maven默认的编译插件的java版本较低,导致其不支持例如泛型,注解等用法. 解决 ...
- angularjs 资源集合
AngularJS是Google开源的一款JavaScript MVC框架,弥补了HTML在构建应用方面的不足. 源码托管在Github上,其通过使用指令(directives)结构来扩展HTML词汇 ...
- German Collegiate Programming Contest 2013:B
一个离散化的简单题: 我用的是STL来做的离散化: 好久没写离散化了,纪念一下! 代码: #include<cstdio> #include<cstring> #include ...
- 树莓派学习路程No.1 GPIO功能初识 wiringPi安装
WiringPi是应用于树莓派平台的GPIO控制库函数,WiringPi遵守GUN Lv3.wiringPi使用C或者C++开发并且可以被其他语言包转,例如python.ruby或者PHP等.Wiri ...
- SQLite 中的各种限制
英文原文:Limits In SQLite 本文定义了 SQLite 的限制,如何针对这些限制定制特定的应用程序.默认的限制设置通常是适当的,几乎适合于每一个应用.有一些应用程序可能需要在 ...
- android和struts2实现android文件上传
1.开发准备如下2个工具类 package org.lxh.util; import java.io.BufferedReader; import java.io.InputStreamReader; ...
- leetcode面试准备:Decode Ways
1 题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: ...
- 【HDOJ】1099 Lottery
题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. #include <cstdio ...
- C++11并发内存模型学习
C++11标准已发布多年,编译器支持也逐渐完善,例如ms平台上从vc2008 tr1到vc2013.新标准对C++改进体现在三方面:1.语言特性(auto,右值,lambda,foreach):2.标 ...