资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Volley框架的使用

   //在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() {

@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);

}
}


成都创新互联2013年开创至今,是专业互联网技术服务公司,拥有项目网站设计制作、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元辽宁做网站,已为上家服务,为辽宁各地企业和个人服务,联系电话:18982081108

//引用示例

/**
 * 判断短信校验码是否正确
 */
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

这个包有源代码的,可以随时更新

有需要的可以联系我


分享名称:Volley框架的使用
链接URL:http://cdkjz.cn/article/gihped.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220