//在Application中初始化publicstaticMyApplicationinstance;publicstaticRequestQueuemRequestQueue;publicvoidonCreate(){super.onCreate();instance=this;mRequestQueue=Volley.newRequestQueue(this);}publicstaticRequestQueuegetQueue(){if(mRequestQueue==null){mRequestQueue=Volley.newRequestQueue(instance);}returnmRequestQueue;}//单例模式中获取唯一的GTApplication实例publicstaticMyApplicationgetInstance(){returninstance;}

//这是一个封装的网络数据请求类packagecom.example.zbclient.util;importorg.json.JSONException;importorg.json.JSONObject;importcom.android.volley.DefaultRetryPolicy;importcom.android.volley.VolleyError;importcom.android.volley.Request.Method;importcom.android.volley.Response.ErrorListener;importcom.android.volley.Response.Listener;importcom.android.volley.toolbox.JsonObjectRequest;importcom.example.zbclient.MyApplication;importcom.google.gson.JsonArray;importandroid.content.Context;importandroid.util.Log;/***网络数据请求**@authoryxx**@date2015-12-23下午7:48:08**/publicclassRequestUtil{publicstaticbooleanisShow=false;/***@paramresres(-1:服务器报错0:成功-2:本地报错)*@paramremark报错内容*@paramjsonArraymsg内的jsonArray数据*/publicstaticabstractclassRequestCallback{publicabstractvoidcallback(Stringres,Stringremark,JSONObjectjsonObject);}publicRequestUtil(Contextcontext){}/***@paramcontext上下文*@paramstrTitle刷新提示内容*@paramflag是否弹出刷新窗口*@paramstrUrl请求地址*@paramjsonObject请求参数*@paramcallback请求数据回调*/publicstaticvoidgetReuestData(finalContextcontext,StringstrTitle,booleanflag,StringstrUrl,JSONObjectjsonObject,finalRequestCallbackcallback){if(flag==true){CommandTools.showProgressDialog(context,strTitle+"");}Log.e("upload",Constant.TestURL+strUrl);Log.v("upload",jsonObject.toString());JsonObjectRequestjsonObjectRequest=newJsonObjectRequest(Method.POST,Constant.TestURL+strUrl,jsonObject.toString(),newListener<JSONObject>(){@OverridepublicvoidonResponse(JSONObjectjsonObject){Log.v("file",jsonObject.toString());StringstrRes=null;StringstrRemark=null;try{strRes=jsonObject.getString("res");strRemark=jsonObject.getString("remark");}catch(JSONExceptione){e.printStackTrace();}finally{CommandTools.dismissProgressDialog();callback.callback(strRes,strRemark,jsonObject);}}},newErrorListener(){@OverridepublicvoidonErrorResponse(VolleyErrorarg0){CommandTools.dismissProgressDialog();callback.callback("-1",arg0.toString(),null);}});jsonObjectRequest.setRetryPolicy(newDefaultRetryPolicy(5*1000,1,1.0f));MyApplication.getQueue().add(jsonObjectRequest);}}


//引用示例

/***判断短信校验码是否正确*/privatevoidcheckSMSCode(){StringstrCode=edtCode.getText().toString();if(strCode==null||strCode.equals("")){CommandTools.showToast(mContext,"验证码不能为空");return;}JSONObjectjsonObject=newJSONObject();try{jsonObject.put("UserID","S1");//学生ID/门店GCODEjsonObject.put("SvsGcode","F1");//验证码场景GCODE00jsonObject.put("VeriCode",strCode);//短信验证码jsonObject.put("OpEmpGcode","E1");//操作人编码jsonObject.put("OpEmpName","王小刚");//操作人jsonObject.put("LoginName","admin");//登录名称jsonObject.put("LoginPwd","1");//登录密码jsonObject.put("AuthSign","fafafdsfds");//权限签名,除登陆外,其他必须有值jsonObject.put("MachineSystem","Android");//请求终端系统:IOS,Android,PDA,OtherjsonObject.put("MachineCode",CommandTools.getMIME(mContext));//机器码}catch(JSONExceptione){e.printStackTrace();}RequestUtil.getReuestData(mContext,"验证码校验中",true,Constant.PostSmsVeriCheck,jsonObject,newRequestCallback(){@Overridepublicvoidcallback(Stringres,Stringremark,JSONObjectjsonObject){if(res.equals("0")==false){CommandTools.showDialog(mContext,remark);return;}CommandTools.showToast(mContext,"验证码校验成功,请输入新密码");flagCode=true;}});}


最重要的千万别忘了在libs下引用volley.jar

这个包有源代码的,可以随时更新

有需要的可以联系我