Android排名DefaultRetryPolicy无法按预期工作

前端之家收集整理的这篇文章主要介绍了Android排名DefaultRetryPolicy无法按预期工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以,我有这个Volley PUT请求:
  1. private boolean syncCall(JSONObject jsonObject,final VolleyCallback
  2. callback) {
  3.  
  4. final ProgressDialog progDailog = new ProgressDialog(context);
  5.  
  6. final Boolean[] success = {false};
  7.  
  8. progDailog.setMessage("...");
  9. progDailog.setIndeterminate(false);
  10. progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  11.  
  12. progDailog.setCancelable(false);
  13. progDailog.show();
  14.  
  15. final SharedPreferences prefs = PreferenceManager
  16. .getDefaultSharedPreferences(context);
  17.  
  18. RequestQueue queue = Volley.newRequestQueue(context,new HurlStack());
  19.  
  20. final String token = prefs.getString("token",null);
  21.  
  22. String URL = Constants.getUrlSync();
  23. String param1 = String.valueOf(prefs.getInt("pmp",1));
  24. String param2 = String.valueOf(prefs.getInt("ei",1));
  25.  
  26. URL = URL.replace("[x]",param1);
  27. URL = URL.replace("[y]",param2);
  28.  
  29. //pegar id pmp e IE corretas
  30. JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request
  31. .Method.PUT,URL,jsonObject,new Response.Listener<JSONObject>() {
  32. @Override
  33. public void onResponse(JSONObject response) {
  34. callback.onSuccess(response + "");
  35. success[0] = true;
  36. progDailog.dismiss();
  37. }
  38. },new Response.ErrorListener() {
  39. @Override
  40. public void onErrorResponse(VolleyError error) {
  41.  
  42. callback.onFailure(error);
  43. tokenFailure(error);
  44. success[0] = false;
  45. progDailog.dismiss();
  46. }
  47. }) {
  48.  
  49.  
  50. @Override
  51. public Map<String,String> getHeaders() throws
  52. AuthFailureError {
  53.  
  54. HashMap<String,String> headers = new HashMap<>();
  55. headers.put("Token",token);
  56.  
  57. return headers;
  58. }
  59. };
  60.  
  61. int socketTimeout = 30000;
  62. RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
  63.  
  64. jsObjRequest.setRetryPolicy(policy);
  65.  
  66.  
  67. queue.add(jsObjRequest);
  68.  
  69. return success[0];
  70. }

我的问题是我发送了一个非常大的JSON,所以5秒的默认超时是不够的.因此,我尝试将超时增加到30秒,并且使用DefaultRetryPolicy来增加重试次数.

问题是,它在5s内保持计时,甚至不会重试一次!

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

解决方法

您需要使用DefaultRetryPolicy吗?

因为你可以定义自己的.

而不是这个:

  1. RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);

试试这个:

  1. jsObjRequest.setRetryPolicy(new RetryPolicy() {
  2. @Override
  3. public int getCurrentTimeout() {
  4. // Here goes the new timeout
  5. return mySeconds;
  6. }
  7. @Override
  8. public int getCurrentRetryCount() {
  9. // The max number of attempts
  10. return myAttempts;
  11. }
  12. @Override
  13. public void retry(VolleyError error) throws VolleyError {
  14. // Here you could check if the retry count has gotten
  15. // To the max number,and if so,send a VolleyError msg
  16. // or something
  17. }
  18. });

猜你在找的Android相关文章