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() {
        @Override
        public void onResponse(String response) {
            //页面跳转
            stopLoading();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("feng", "error");
            stopLoading();
        }
    }) {
        @Override
        protected Map getParams() throws AuthFailureError {
            Map map = new HashMap();
            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() {
                @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 getParams() throws AuthFailureError {
                    Map winType = new HashMap();
                    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
));

你可能感兴趣的:(Android)