您好,登錄后才能下訂單哦!
//在Application中初始化 public static MyApplication instance; public static RequestQueue mRequestQueue; public void onCreate() { super.onCreate(); instance = this; mRequestQueue = Volley.newRequestQueue(this); } public static RequestQueue getQueue(){ if(mRequestQueue == null){ mRequestQueue = Volley.newRequestQueue(instance); } return mRequestQueue; } // 單例模式中獲取唯一的GTApplication實例 public static MyApplication getInstance() { return instance; }
//這是一個封裝的網(wǎng)絡(luò)數(shù)據(jù)請求類 package com.example.zbclient.util; import org.json.JSONException; import org.json.JSONObject; 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.google.gson.JsonArray; import android.content.Context; 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){ if(flag == true){ CommandTools.showProgressDialog(context, strTitle + ""); } Log.e("upload", Constant.TestURL + strUrl); Log.v("upload", jsonObject.toString()); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST, Constant.TestURL + strUrl, jsonObject.toString(), new Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { Log.v("file", jsonObject.toString()); String strRes = null; String strRemark = null; try { strRes = jsonObject.getString("res"); strRemark = jsonObject.getString("remark"); } catch (JSONException e) { e.printStackTrace(); }finally{ CommandTools.dismissProgressDialog(); callback.callback(strRes, strRemark, jsonObject); } } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { CommandTools.dismissProgressDialog(); callback.callback("-1", arg0.toString(), null); } }); jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(5 * 1000, 1, 1.0f)); MyApplication.getQueue().add(jsonObjectRequest); } }
//引用示例
/** * 判斷短信校驗碼是否正確 */ private void checkSMSCode(){ String strCode = edtCode.getText().toString(); if(strCode == null || strCode.equals("")){ CommandTools.showToast(mContext, "驗證碼不能為空"); return; } JSONObject jsonObject = new JSONObject(); try { jsonObject.put("UserID", "S1"); //學(xué)生ID/門店GCODE jsonObject.put("SvsGcode", "F1");//驗證碼場景GCODE 00 jsonObject.put("VeriCode", strCode);//短信驗證碼 jsonObject.put("OpEmpGcode", "E1");//操作人編碼 jsonObject.put("OpEmpName", "王小剛");//操作人 jsonObject.put("LoginName", "admin");//登錄名稱 jsonObject.put("LoginPwd", "1");//登錄密碼 jsonObject.put("AuthSign", "fafafdsfds");//權(quán)限簽名,除登陸外,其他必須有值 jsonObject.put("MachineSystem", "Android");//請求終端系統(tǒng): IOS,Android,PDA,Other jsonObject.put("MachineCode", CommandTools.getMIME(mContext));//機器碼 } catch (JSONException e) { e.printStackTrace(); } RequestUtil.getReuestData(mContext, "驗證碼校驗中", true, Constant.PostSmsVeriCheck, jsonObject, new RequestCallback() { @Override public void callback(String res, String remark, JSONObject jsonObject) { if(res.equals("0") == false){ CommandTools.showDialog(mContext, remark); return; } CommandTools.showToast(mContext, "驗證碼校驗成功, 請輸入新密碼"); flagCode = true; } }); }
最重要的千萬別忘了在libs下引用volley.jar
這個包有源代碼的,可以隨時更新
有需要的可以聯(lián)系我
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。