volley Request添加Header的HTTP请求
2016-02-24 18:15
477 查看
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<JSONObject>() { @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<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); 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); } }
相关文章推荐
- 探秘ajax跨域请求
- PHP实现支持GET,POST,Multipart/form-data的HTTP请求类
- 实例详解jQuery Mockjax 插件模拟 Ajax 请求
- Jquery Ajax请求方法小结(值得收藏)
- Ajax在请求过程中显示进度的简单实现
- jquery中AJAX请求 $.post方法的使用
- Javascript跨域请求的4种解决方式
- 跟我学Laravel之请求与输入
- php发送post请求的三种方法
- Ajax请求内嵌套Ajax请求示例代码
- 优化IIS7.5支持10万个同时请求的配置方法
- 浅析jQuery Ajax请求参数和返回数据的处理
- Android客户端post请求服务器端实例
- Android的HTTP类库Volley入门学习教程
- Android使用缓存机制实现文件下载及异步请求图片加三级缓存
- Ajax请求过程中下载文件在FireFox(火狐)浏览器下的兼容问题
- 跟我学Laravel之请求(Request)的生命周期
- 微信公众平台开发之配置与请求
- php curl模拟post请求和提交多维数组的示例代码
- ajax取消挂起请求的处理方法