Volley的简单二次封装

新建一个application

 1 package com.honghe.myvolley.app;
 2 
 3 import com.android.volley.RequestQueue;
 4 import com.android.volley.toolbox.Volley;
 5 
 6 import android.app.Application;
 7 
 8 public class MyApplication extends Application {
 9     private static RequestQueue queues;
10 
11     @Override
12     public void onCreate() {
13         super.onCreate();
14         queues = Volley.newRequestQueue(getApplicationContext());
15     }
16 
17     public static RequestQueue getHttpQueue() {
18 
19         return queues;
20     }
21 
22 }

在xml文件中注册为启动的application

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.honghe.myvolley"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="22" />
10     <uses-permission android:name="android.permission.INTERNET"/>
11 
12     <application
13         android:name="com.honghe.myvolley.app.MyApplication"
14         android:allowBackup="true"
15         android:icon="@drawable/ic_launcher"
16         android:label="@string/app_name" >
17         <activity
18             android:name="com.honghe.myvolley.Activity.MainActivity"
19             android:label="@string/app_name" >
20             <intent-filter>
21                 <action android:name="android.intent.action.MAIN" />
22 
23                 <category android:name="android.intent.category.LAUNCHER" />
24             </intent-filter>
25         </activity>
26     </application>
27 
28 </manifest>

编写volleyrequest类,实现get和post方法

 1 package com.honghe.myvolley.Volley;
 2 
 3 import java.util.Map;
 4 
 5 import android.content.Context;
 6 
 7 import com.android.volley.AuthFailureError;
 8 import com.android.volley.Request.Method;
 9 import com.android.volley.toolbox.StringRequest;
10 import com.honghe.myvolley.app.MyApplication;
11 
12 public class VolleyRequest {
13     public static StringRequest stringRequest;
14     public static Context context;
15 
16     public static void RequestGet(Context mContext, String url, String tag,
17             VolleyInterface vif) {
18         MyApplication.getHttpQueue().cancelAll(tag);
19         stringRequest = new StringRequest(Method.GET, url,
20                 vif.loadingListener(), vif.errorListener());
21         stringRequest.setTag(tag);
22         MyApplication.getHttpQueue().add(stringRequest);
23         MyApplication.getHttpQueue().start();
24     }
25 
26     public static void RequestPost(Context mContext, String url, String tag,
27             final Map<String, String> params, VolleyInterface vif) {
28         MyApplication.getHttpQueue().cancelAll(tag);
29         stringRequest = new StringRequest(url, vif.loadingListener(),
30                 vif.errorListener()) {
31 
32             @Override
33             protected Map<String, String> getParams() throws AuthFailureError {
34                 
35                 return params;
36             }
37 
38         };
39 
40         stringRequest.setTag(tag);
41         MyApplication.getHttpQueue().add(stringRequest);
42         MyApplication.getHttpQueue().start();
43     }
44 
45 }

设置volleyInterface的回调

 1 package com.honghe.myvolley.Volley;
 2 
 3 import android.content.Context;
 4 
 5 import com.android.volley.Response.ErrorListener;
 6 import com.android.volley.Response.Listener;
 7 import com.android.volley.VolleyError;
 8 
 9 public abstract class VolleyInterface {
10 
11     public Context mContext;
12     public Listener<String> mListener;
13     public ErrorListener mErrorListener;
14 
15     public VolleyInterface(Context context) {
16         this.mContext = context;
17     }
18 
19     public Listener<String> loadingListener() {
20         mListener = new Listener<String>() {
21 
22             @Override
23             public void onResponse(String arg0) {
24                 OnMySuccess(arg0);
25             }
26         };
27         return mListener;
28     }
29 
30     public ErrorListener errorListener() {
31 
32         mErrorListener = new ErrorListener() {
33 
34             @Override
35             public void onErrorResponse(VolleyError arg0) {
36                 OnMyError(arg0);
37             }
38         };
39         return mErrorListener;
40     }
41 
42     public abstract void OnMySuccess(String result);
43 
44     public abstract void OnMyError(VolleyError arg0);
45 }

如何使用

 1 protected void VolleyGet(String url, String tag) {
 2         VolleyRequest.RequestGet(this, url, tag, new VolleyInterface(this) {
 3 
 4             @Override
 5             public void OnMySuccess(String result) {
 6 
 7             }
 8 
 9             @Override
10             public void OnMyError(VolleyError arg0) {
11 
12             }
13         });
14     }

 

posted @ 2015-12-28 11:54  dongweiq  阅读(542)  评论(0编辑  收藏  举报