Android volley sending data twice

后端 未结 5 1075
别跟我提以往
别跟我提以往 2020-11-30 03:12

I am using Volley Network Library in my application.

The Issue is that it is sending data more than once when network connection is slow.

相关标签:
5条回答
  • 2020-11-30 03:20

    Use below code after completion of new Response.ErrorListener() method in your volley parsing. Hope its useful for you. I faced same issue and resolved it with the same code.

    Code:

    jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(
                    30000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    
    0 讨论(0)
  • 2020-11-30 03:20

    Try this will definitely work.

     public <T> void addToRequestQueue(StringRequest req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        req.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    
        getRequestQueue().add(req);
    }
    
    0 讨论(0)
  • 2020-11-30 03:27

    This will work

     RetryPolicy mRetryPolicy = new DefaultRetryPolicy(
     0,
     DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
     DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    
     request.setRetryPolicy(mRetryPolicy);
    
    0 讨论(0)
  • 2020-11-30 03:29

    I solve this problem to use this code. Set getCurrentRetryCount() to return 0 and Use HurlStack for BaseHttpStack.

    RequestQueue requestQueue = Volley.newRequestQueue(NavigationActivity.this, new HurlStack());
        requestQueue.add(stringRequest).setRetryPolicy(new RetryPolicy() {
            @Override
            public int getCurrentTimeout() {
                return 5000;
            }
    
            @Override
            public int getCurrentRetryCount() {
                return 0; //retry turn off
            }
    
            @Override
            public void retry(VolleyError error) throws VolleyError {
    
            }
        });
    

    Hope this solve the problem.

    0 讨论(0)
  • 2020-11-30 03:37

    No need to use connection.setChunkedStreamingMode(0); to avoid volley sending data twice bug. you need to set retry policy for current request :

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(...);
    jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
                           0,
                           DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                           DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    
    0 讨论(0)
提交回复
热议问题