温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

volley Request添加Header的HTTP请求

发布时间:2020-05-20 21:19:30 来源:网络 阅读:2619 作者:671076656 栏目:移动开发
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);
}
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI