Android排名DefaultRetryPolicy无法按预期工作

Car*_*los 6 android android-volley

所以,我有这个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];
}
Run Code Online (Sandbox Code Playgroud)

我的问题是我发送的非常大JSON,所以5秒的默认超时是不够的.因此,我尝试将超时时间增加到30秒,然后搞乱DefaultRetryPolicy增加重试次数.

问题是,它保持timeouting5秒,甚至没有重试一次!

我是否必须有一个监听器或回调重试?我做错了DefaultRetryPolicy什么?请帮忙,这个问题让我疯狂......

her*_*ell 5

您需要使用DefaultRetryPolicy吗?

因为你可以定义自己的.

而不是这个:

RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,     
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,     
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
Run Code Online (Sandbox Code Playgroud)

试试这个:

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    
        }
    });
Run Code Online (Sandbox Code Playgroud)