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;/** * Network data request * * @author yxx * * @date 2015-12-23 pm 7:48:08 * */public class requestutil{public static boolean isshow = false;/** * @param resres (-1: Server error &NBsp; 0: Success -2: local error) * @param remark error content * @param Jsonarray data */public static abstract class requestcallback within the JSONARRAY&NBSP;&NBSP;MSG {public abstract void callback (String res, string remark, jsonobject jsonobject);} Public requestutil (context context) {}/** * @param context context * @param strTitle Refresh prompt content * @param flag popup Refresh window * @param strUrl request Address * @param jsonObject Request Parameters * @param callback request data 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, "Network error, please check network Configuration"); return;} if (flag == true) {customprogress.showdialog (context, strtitle, true, null);} LOG.E ("Upload", "---------------------------------------------"); LOG.I ("Upload", "action: " + 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> () {@Overridepublic void onresponse (jsonobject jsonobject) {log.v ("File", Jsonobject.tostring ()); Customprogress.dissdialog (); string strres = null; String strremark =&nbSp;null;try {strres = jsonobject.getstring ("res"); Strremark = jsonobject.getstring (" Remark ");} catch (jsonexception e) {e.printstacktrace ();} Finally{callback.callback (Strres, strremark, jsonobject);}}}, new errorlistener () {@ Overridepublic 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);}}
This article is from the "climb over the mountains to see the Sea" blog, please be sure to keep this source http://670176656.blog.51cto.com/4500575/1744734
Volley request to add header HTTP requests