volleyRequest添加Header的HTTP请求
package com.example.zbclient.util; import java.util.HashMap; import java.util.Map; import org.json.JSONException; import org.json.JSONObject; import com.android.volley.AuthFailureError; import com.android.volley.DefaultRetryPolicy; import com.android.volley.VolleyError; import com.android.volley.Request.Method; import com.android.volley.Response.ErrorListener; import com.android.volley.Response.Listener; import com.android.volley.toolbox.JsonObjectRequest; import com.example.zbclient.MyApplication; import com.example.zbclient.encryption.MCrypt; import android.content.Context; import android.util.Base64; import android.util.Log; /** * 网络数据请求 * * @author yxx * * @date 2015-12-23 下午7:48:08 * */ public class RequestUtil{ public static boolean isShow = false; /** * @param resres (-1:服务器报错 0: 成功 -2:本地报错) * @param remark 报错内容 * @param jsonArray msg内的jsonArray数据 */ public static abstract class RequestCallback { public abstract void callback(String res, String remark, JSONObject jsonObject); } public RequestUtil(Context context){ } /** * @param context 上下文 * @param strTitle 刷新提示内容 * @param flag 是否弹出刷新窗口 * @param strUrl 请求地址 * @param jsonObject 请求参数 * @param callback 请求数据回调 */ public static void getReuestData(final Context context, String strTitle, boolean flag, String strUrl, JSONObject jsonObject, final RequestCallback callback){ 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", "---------------------------------------------"); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST, Constant.FormalURL + strUrl, jsonObject.toString(), new Listener() { @Override public void onResponse(JSONObject jsonObject) { Log.v("file", jsonObject.toString()); CustomProgress.dissDialog(); String strRes = null; String strRemark = null; try { strRes = jsonObject.getString("res"); strRemark = jsonObject.getString("remark"); } catch (JSONException e) { e.printStackTrace(); }finally{ callback.callback(strRes, strRemark, jsonObject); } } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { CustomProgress.dissDialog(); callback.callback("-1", arg0.getMessage() + "", null); } }) { @Override public Map getHeaders() throws AuthFailureError { HashMap headers = new HashMap (); try { MCrypt mCrypt = new MCrypt(); 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 (Exception e) { e.printStackTrace(); } return headers; } }; jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(5 * 1000, 1, 1.0f)); MyApplication.getQueue().add(jsonObjectRequest); } }
本文标题:volleyRequest添加Header的HTTP请求
文章转载:http://myzitong.com/article/piisjg.html