您好,登錄后才能下訂單哦!
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); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。