码迷,mamicode.com
首页 > Web开发 > 详细

volley Request添加Header的HTTP请求

时间:2016-02-24 19:35:57      阅读:623      评论:0      收藏:0      [点我收藏+]

标签:volley

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);
}
}

本文出自 “爬过山见过海” 博客,请务必保留此出处http://670176656.blog.51cto.com/4500575/1744734

volley Request添加Header的HTTP请求

标签:volley

原文地址:http://670176656.blog.51cto.com/4500575/1744734

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!