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;
/**
* 網(wǎng)絡(luò)數(shù)據(jù)請求
*
* @author yxx
*
* @date 2015-12-23 下午7:48:08
*
*/
public class RequestUtil{
public static boolean isShow = false;
/**
* @param resres (-1:服務(wù)器報錯 0: 成功 -2:本地報錯)
* @param remark 報錯內(nèi)容
* @param jsonArray msg內(nèi)的jsonArray數(shù)據(jù)
*/
public static abstract class RequestCallback {
public abstract void callback(String res, String remark, JSONObject jsonObject);
}
public RequestUtil(Context context){
}
/**
* @param context 上下文
* @param strTitle 刷新提示內(nèi)容
* @param flag 是否彈出刷新窗口
* @param strUrl 請求地址
* @param jsonObject 請求參數(shù)
* @param callback 請求數(shù)據(jù)回調(diào)
*/
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, "網(wǎng)絡(luò)錯誤,請檢查網(wǎng)絡(luò)配置");
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);
}
}
分享題目:volleyRequest添加Header的HTTP請求
本文鏈接:http://www.chinadenli.net/article26/piisjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、全網(wǎng)營銷推廣、建站公司、關(guān)鍵詞優(yōu)化、網(wǎng)站制作、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)