Volley详解、的get和post请求、请求多次和超时

1、今天使用volley进行数据请求,get请求比较简单,post请求时,后台接口人员写了一个接收的是实体bean,就想了几个方法,发现都有问题,自己还是对数据请求这方面不够精通啊,只做自己记录

2、具体代码

/*保存客户数据信息*/
private void saveClientData(){
    startLoading();
    URL = new StringBuffer(Transition.BASEURL + "mobile/addClient");
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL.toString(), new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //页面跳转
            stopLoading();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("feng", "error");
            stopLoading();
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> map = new HashMap<String, String>();
            map.put("clientName", customBean.getClientName());
            map.put("clientPhone", customBean.getClientPhone());
            return map;
        }
    };
    MyVolley.addRequest(stringRequest);
}

3、后台通过json的方式进行获取的

//将保存画面中内容的journalBean上传至服务器
private void jouranlDataUpLoad(){
    ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cwjManager.getActiveNetworkInfo();
    if (info != null && info.isAvailable()) {
        volleyString.clear();
        //设置圆圈转动
        dataLodingDialog=new DataLodingDialog();
        dataLodingDialog.start(JournalDetailActivity.this);
        try {
            URL = new StringBuffer(Transition.langUrl + "/syncUser!uploadJournalData.do");
            StringRequest stringRequest = new StringRequest(Request.Method.POST, URL.toString(), new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    //页面跳转
                    volleyFinishUpload("jouranlDataUpLoad");
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i("feng", "error");
                    volleyFinishUpload("jouranlDataUpLoad");
                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> winType = new HashMap<String, String>();
                    try {
                        String journalString = journalUpload();
                        winType.put("journal", journalString);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return winType;
                }
            };
            MyVolley.addRequest(stringRequest);
            volleyString.put("jouranlDataUpLoad","0");
        } catch (Exception e) {
            Log.i("feng", e.toString());
            Helper.saveFileLog("JournalDetailActivity_jouranlDataUpLoad__"+e.toString());
            Intent intent =new Intent();
            IntentJumpHelper.IntentJump(intent,JournalDetailActivity.this,IndexActivity.class,0);
        }
    }else{
        Toast.makeText(JournalDetailActivity.this, "保存数据失败", Toast.LENGTH_SHORT).show();
    }
}

4、多次请求和请求超时时间设置

request.setRetryPolicy( new DefaultRetryPolicy(
        10*1000,//默认超时时间,应设置一个稍微大点儿的,例如本处的500000
        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,//默认最大尝试次数
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值