Android Volley DefaultRetryPolicy无法正常工作 [英] Android volley DefaultRetryPolicy do not work as intended

查看:184
本文介绍了Android Volley DefaultRetryPolicy无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有以下Volley PUT请求:

So, I have this Volley PUT request:

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来增加重试次数.

My problem is that I send a very large JSON, so the default timeout of 5 seconds is not enough. So, I tried to increase the timeout to 30 seconds and messing with the DefaultRetryPolicy to increase the number of retries.

问题是,它将timeouting保留5秒钟,甚至不重试一次!

The thing is, it keeps timeouting in 5s and it doesn't even retry once!

我必须有一个重试的监听器或回调吗?我在DefaultRetryPolicy上做错了什么?请帮助,这个问题使我发疯...

Do I have to have a listener or a callback for the retries ? I'm doing something wrong with the DefaultRetryPolicy ? Please help, this issue is driving me nuts...

推荐答案

是否需要使用DefaultRetryPolicy?

Do you need to use the DefaultRetryPolicy?

因为您可以定义自己的

代替此:

RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,     
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,     
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    
        }
    });

这篇关于Android Volley DefaultRetryPolicy无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆