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); } }
volley Request添加Header的HTTP请求
原创
©著作权归作者所有:来自51CTO博客作者671076656的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:PopupWindow简单用法

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
HTTP Request header
HTTP Request header当今web程序的开发技术真是百家争鸣,
http 服务器 客户端 数据 -
HTTP Request header
<!DOCTYPE html PUBLIC "-
服务器 客户端 数据 -
Volley的基本使用(1)——http请求
Volley框架有如下特点:1.Volley是谷歌官方发不的第三方框架2.使用Volley通信更快更简单3.Volley中的get
android开发 网络 json ide android