今天看了個講Vollery框架的視頻,其中有對StringRequest作了個簡單封裝的部分,感受不錯,但只是視頻,搜了下,沒搜到相關代碼,(嘿嘿,平時作慣了伸手黨),如今把視頻中封裝的地方整理了下,供你們參考下。java
首先定義一個全局的Vollery請求隊列,這裏放到自定義的Application中android
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進行二次封裝,首先定義一個抽象接口類markdown
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<String> mListener; public static ErrorListener mErrorListener; public VolleyInterface(Context Context, Listener<String> 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<String> loadListener() { this.mListener = new Listener<String>() { @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的二次封裝app
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<String, String> param, VolleyInterface vif) { // 取消其餘請求 MyApplication.getHttpQueues().cancelAll(tag); stringRequest = new StringRequest(url, vif.loadListener(), vif.errorListener()) { @Override protected Map<String, String> 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<String, String> param = new HashMap<String,String>(); 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自身參數的設置操做,只需傳入指定參數便可(通常封裝都會包含此目的),實際上是能夠對請求統一處理,例如請求數據前加載進度條,或者一些其餘公用的預處理,失敗的時候統一處理等等,請求統一處理這塊歸功於對接口回調的封裝。封裝看上去很簡單,但讓本身來寫,也不是隨時就來的,功夫有待增強,學習中~~~ide
版權聲明:本文爲博主原創文章,未經博主容許不得轉載。post