在Application初始化 Volley queues=Volley.newRequestQueue(appContext);
并返回RequestQueue 对象
public static RequestQueue getHttpQueues(){
return queues;
}
import android.content.Context; import com.android.volley.Response.ErrorListener; import com.android.volley.Response.Listener; import com.android.volley.VolleyError; importpublic abstract class VolleyInterface { public Context context; public static Listener<ResultModel> mListener; public static ErrorListener mErrorListener; public VolleyInterface(Context context,Listener<ResultModel> lisener,ErrorListener errorLisener){ this.context=context; mListener=lisener; mErrorListener=errorLisener; } public Listener<ResultModel> loadingListener(){ mListener=new Listener<ResultModel>() { @Override public void onResponse(ResultModel resultModel) { onMySuccess(resultModel); } }; return mListener; } public ErrorListener errorListener(){ mErrorListener=new ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { OnMyError(volleyError); } }; return mErrorListener; } public abstract void onMySuccess(ResultModel resultModel); public abstract void OnMyError(VolleyError volleyError); }
VolleyRequest类
import java.util.Map; import android.content.Context; import com.android.volley.Request.Method; importimportpublic class VolleyRequest { private static FastJsonRequest<ResultModel> fastJsonRequest; public static Context context; public static void RequestGet(Context mContext,String url,String tag,VolleyInterface vif){ FandDaApplication.getHttpQueues().cancelAll(tag); fastJsonRequest=new FastJsonRequest<ResultModel>(url, ResultModel.class, vif.loadingListener(), vif.errorListener()); fastJsonRequest.setTag(tag); FandDaApplication.getHttpQueues().add(fastJsonRequest); //调用会引发com.android.volley.NoConnectionError: java.io.InterruptedIOException错误,原因是volley已经调用过了 //FandDaApplication.getHttpQueues().start(); } public static void RequestPost(Context context,String url,Map<String,String> map,String tag,VolleyInterface vif){ FandDaApplication.getHttpQueues().cancelAll(tag); fastJsonRequest=new FastJsonRequest<ResultModel>(Method.POST,url, ResultModel.class, map,vif.loadingListener(), vif.errorListener()); fastJsonRequest.setTag(tag); FandDaApplication.getHttpQueues().add(fastJsonRequest); //调用会引发com.android.volley.NoConnectionError: java.io.InterruptedIOException错误,原因是volley已经调用过了 //FandDaApplication.getHttpQueues().start(); } }
使用方式
String url=new BroadcastAPI().getBroadcastList(0,"",0,"",page,pageSize); VolleyRequest.RequestGet(context, url, "NEWINVILIST", new VolleyInterface(context,VolleyInterface.mListener,VolleyInterface.mErrorListener) { @Override public void onMySuccess(ResultModel resultModel) { if(ResultApi.isCode(resultModel)){ list =JSON.parseArray(resultModel.getResult().toString(), BroadcastDetailsModel.class); //如果用户刷新数据则清空原来缓存记录,为了保证数据统一性 BroadcastTable.getInstance(context).deleteAllData(BroadcastTable.newNumber); if (mType == 1) { adapter.addItemTop(list); } else { adapter.addItemLast(list); } BroadcastTable.getInstance(context).saveBroadcastList(list,BroadcastTable.newNumber); }else{ if(mType==1){ ToastUtil.show(context, getString(R.string.toast_empty_data)); }else{ ToastUtil.show(context, getString(R.string.toast_next_empty)); } } mSwipeRefreshWidget.setRefreshing(false); } @Override public void OnMyError(VolleyError volleyError) { ToastUtil.show(context, volleyError.getMessage()); mSwipeRefreshWidget.setRefreshing(false); } });
public class ResultModel { private int code; private String msg; private Object result; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getResult() { return result; } public void setResult(Object result) { this.result = result; }