volley Request添加Header的HTTP请求
packagecom.example.zbclient.util;importjava.util.HashMap;importjava.util.Map;importorg.json.JSONException;importorg.json.JSONObject;importcom.android.volley.AuthFailureError;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.example.zbclient.encryption.MCrypt;importandroid.content.Context;importandroid.util.Base64;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){MyApplication.getInstance().mRandom=CommandTools.CeShi();MyApplication.getInstance().sendTime=CommandTools.initDataTime();if(CommandTools.isNetworkAvailable(context)==false){CommandTools.showToast(context,"网络错误,请检查网络配置");return;}if(flag==true){CustomProgress.showDialog(context,strTitle,true,null);}Log.e("upload","---------------------------------------------");Log.i("upload","动作:"+strTitle);Log.i("upload",Constant.FormalURL+strUrl);Log.i("upload",jsonObject.toString());Log.e("upload","---------------------------------------------");JsonObjectRequestjsonObjectRequest=newJsonObjectRequest(Method.POST,Constant.FormalURL+strUrl,jsonObject.toString(),newListener<JSONObject>(){@OverridepublicvoidonResponse(JSONObjectjsonObject){Log.v("file",jsonObject.toString());CustomProgress.dissDialog();StringstrRes=null;StringstrRemark=null;try{strRes=jsonObject.getString("res");strRemark=jsonObject.getString("remark");}catch(JSONExceptione){e.printStackTrace();}finally{callback.callback(strRes,strRemark,jsonObject);}}},newErrorListener(){@OverridepublicvoidonErrorResponse(VolleyErrorarg0){CustomProgress.dissDialog();callback.callback("-1",arg0.getMessage()+"",null);}}){@OverridepublicMap<String,String>getHeaders()throwsAuthFailureError{HashMap<String,String>headers=newHashMap<String,String>();try{MCryptmCrypt=newMCrypt();headers.put("accept","text/json");headers.put("sendtime",MyApplication.getInstance().sendTime);headers.put("sign",Base64.encodeToString(mCrypt.encrypt(MyApplication.getInstance().mSign),Base64.NO_WRAP)+MyApplication.getInstance().mRandom);}catch(Exceptione){e.printStackTrace();}returnheaders;}};jsonObjectRequest.setRetryPolicy(newDefaultRetryPolicy(5*1000,1,1.0f));MyApplication.getQueue().add(jsonObjectRequest);}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。