//在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;
}
//这是一个封装的网络数据请求类
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;
/**
* 网络数据请求
*
* @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){
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"); //学生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");//权限签名,除登陆外,其他必须有值
jsonObject.put("MachineSystem", "Android");//请求终端系统: 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
这个包有源代码的,可以随时更新
有需要的可以联系我
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。