所以,我有这个Volley PUT请求:
private boolean syncCall(JSONObject jsonObject,final VolleyCallback callback) { final ProgressDialog progDailog = new ProgressDialog(context); final Boolean[] success = {false}; progDailog.setMessage("..."); progDailog.setIndeterminate(false); progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progDailog.setCancelable(false); progDailog.show(); final SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(context); RequestQueue queue = Volley.newRequestQueue(context,new HurlStack()); final String token = prefs.getString("token",null); String URL = Constants.getUrlSync(); String param1 = String.valueOf(prefs.getInt("pmp",1)); String param2 = String.valueOf(prefs.getInt("ei",1)); URL = URL.replace("[x]",param1); URL = URL.replace("[y]",param2); //pegar id pmp e IE corretas JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request .Method.PUT,URL,jsonObject,new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { callback.onSuccess(response + ""); success[0] = true; progDailog.dismiss(); } },new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { callback.onFailure(error); tokenFailure(error); success[0] = false; progDailog.dismiss(); } }) { @Override public Map<String,String> getHeaders() throws AuthFailureError { HashMap<String,String> headers = new HashMap<>(); headers.put("Token",token); return headers; } }; int socketTimeout = 30000; RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); jsObjRequest.setRetryPolicy(policy); queue.add(jsObjRequest); return success[0]; }
我的问题是我发送了一个非常大的JSON,所以5秒的默认超时是不够的.因此,我尝试将超时增加到30秒,并且使用DefaultRetryPolicy来增加重试次数.
问题是,它在5s内保持计时,甚至不会重试一次!
我是否必须有一个监听器或回调重试?我在使用DefaultRetryPolicy做错了什么?请帮忙,这个问题让我疯狂……
解决方法
您需要使用DefaultRetryPolicy吗?
因为你可以定义自己的.
而不是这个:
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
试试这个:
jsObjRequest.setRetryPolicy(new RetryPolicy() { @Override public int getCurrentTimeout() { // Here goes the new timeout return mySeconds; } @Override public int getCurrentRetryCount() { // The max number of attempts return myAttempts; } @Override public void retry(VolleyError error) throws VolleyError { // Here you could check if the retry count has gotten // To the max number,and if so,send a VolleyError msg // or something } });