今天看了个讲Vollery框架的视频,其中有对StringRequest做了个简单封装的部分,感觉不错,但只是视频,搜了下,没搜到相关代码,(嘿嘿,平时做惯了伸手党),现在把视频中封装的地方整理了下,供大家参考下。
首先定义一个全局的Vollery请求队列,这里放到自定义的Application中
package com.qianmeng.myvolley;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import android.app.Application;
/**
*
* 自定义Application
*
* @author qianmeng
*
*/
public class MyApplication extends Application {
//全局请求队列
public static RequestQueue queues;
@Override
public void onCreate() {
super.onCreate();
queues = Volley.newRequestQueue(getApplicationContext());
}
public static RequestQueue getHttpQueues()
{
return queues;
}
}
然后开始对Vollery进行二次封装,首先定义一个抽象接口类
package com.qianmeng.myvolley;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import android.content.Context;
/**
* Volley二次封装抽象类
*
* @author qianmeng
*
*/
public abstract class VolleyInterface {
public Context mContext;
public static Listener mListener;
public static ErrorListener mErrorListener;
public VolleyInterface(Context Context, Listener listener,
ErrorListener errorListener) {
this.mContext = Context;
this.mListener = listener;
this.mErrorListener = errorListener;
}
//对外开放接口
public abstract void onMySuccess(String result);
public abstract void onMyError(VolleyError error);
//请求成功接口
public Listener loadListener() {
this.mListener = new Listener() {
@Override
public void onResponse(String arg0) {
onMySuccess(arg0);
}
};
return mListener;
}
//请求失败接口
public ErrorListener errorListener() {
this.mErrorListener = new ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
onMyError(arg0);
}
};
return mErrorListener;
}
}
通过这个抽象接口类进行StringRequest的二次封装
package com.qianmeng.myvolley;
import java.util.Map;
import android.content.Context;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.StringRequest;
/**
* Volley请求二次封装
*
* @author qianmeng
*
*/
public class VolleyRequest {
public static StringRequest stringRequest;
public static Context context;
public static void requestGet(Context mContext, String url, String tag,
VolleyInterface vif) {
// 取消其他请求
MyApplication.getHttpQueues().cancelAll(tag);
stringRequest = new StringRequest(Method.GET, url, vif.loadListener(),
vif.errorListener());
//设置请求标签
stringRequest.setTag(tag);
//添加并启动请求队列
MyApplication.getHttpQueues().add(stringRequest);
MyApplication.getHttpQueues().start();
}
public static void requestPost(Context mContext, String url, String tag,
final Map param, VolleyInterface vif) {
// 取消其他请求
MyApplication.getHttpQueues().cancelAll(tag);
stringRequest = new StringRequest(url, vif.loadListener(),
vif.errorListener()) {
@Override
protected Map getParams() {
return param;
}
};
//设置请求标签
stringRequest.setTag(tag);
//添加并启动请求队列
MyApplication.getHttpQueues().add(stringRequest);
MyApplication.getHttpQueues().start();
}
}
最后就可以在Activity中很方便的进行调用。
package com.qianmeng.myvolley;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.android.volley.VolleyError;
/**
* 应用主界面
*
* @author qianmeng
*
*/
public class MainActivity extends Activity implements OnClickListener {
private String url="http://www.baidu.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
//初始化控件
private void initView(){
final Button btnGet=(Button) findViewById(R.id.btn_get);
final Button btnPost=(Button) findViewById(R.id.btn_post);
btnGet.setOnClickListener(this);
btnPost.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_get:
initGet();
break;
case R.id.btn_post:
initPost();
break;
default:
break;
}
}
//Get请求
private void initGet(){
VolleyRequest.requestGet(this, url, "doGet", new VolleyInterface(this,VolleyInterface.mListener,VolleyInterface.mErrorListener) {
@Override
public void onMySuccess(String result) {
Toast.makeText(MainActivity.this, "Get Success", Toast.LENGTH_LONG).show();
}
@Override
public void onMyError(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
});
}
//Post请求
private void initPost(){
//请求参数
Map param = new HashMap();
param.put("id", "2");
param.put("name", "qianmeng");
VolleyRequest.requestPost(this, url, "doPost", param, new VolleyInterface(this,VolleyInterface.mListener,VolleyInterface.mErrorListener) {
@Override
public void onMySuccess(String result) {
Toast.makeText(MainActivity.this, "Post Success", Toast.LENGTH_LONG).show();
}
@Override
public void onMyError(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
});
}
@Override
protected void onStop() {
//Activity 销毁时取消请求
MyApplication.getHttpQueues().cancelAll("doGet");
MyApplication.getHttpQueues().cancelAll("doPost");
super.onStop();
}
}
封装之后的好处就是省去了一些Vollery自身参数的设置操作,只需传入指定参数即可(一般封装都会包含此目的),其实是可以对请求统一处理,例如请求数据前加载进度条,或者一些其他公用的预处理,失败的时候统一处理等等,请求统一处理这块归功于对接口回调的封装。封装看上去很简单,但让自己来写,也不是随时就来的,功夫有待加强,学习中~~~