Android Volley double post when have slow request

后端 未结 18 2686
生来不讨喜
生来不讨喜 2020-11-28 04:06

I have a problem with Volley POST request on slow network. Everytime I see BasicNetwork.logSlowRequests in my LogCat, my POST request is executed twice or more

相关标签:
18条回答
  • 2020-11-28 04:34

    It's a problem of timeout error The request is execute twice so you have a double value

    Try to increase the timeout error request with this code :

    request.setRetryPolicy(new DefaultRetryPolicy(
    DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2,
    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    
    0 讨论(0)
  • 2020-11-28 04:34

    Volley request policy only one time request to avoid duplicate post

    I tried this solution, but does not working

    //...........solution 01
    jsonObjectRequest.retryPolicy = DefaultRetryPolicy(
    120000, 
    0,
    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
    
    
    or
    
    //...........solution 02
    jsonObjectRequest.retryPolicy = DefaultRetryPolicy(
    0, 
    -1,
    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
    
    
    MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest)
    

    Full Source Code: https://androidkeynotes.blogspot.com/2020/02/volley.html

    0 讨论(0)
  • 2020-11-28 04:35

    just maxNumRetries = 0 no work. set TIMEOUT_MS 20000.

    request.setRetryPolicy(new DefaultRetryPolicy(20000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    
    0 讨论(0)
  • 2020-11-28 04:36

    I am able to solve this issue by two ways.

    First is changed the RetryPolicy. Simply set the timeout value to double of the default timeout. Worked fine. You can also try other values.

    request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    

    Another way is by setting connection.setChunkedStreamingMode(0); in openConnection method HurlStack class.

    I am creating my RequestQueue like this requestQueue = Volley.newRequestQueue(context, new HurlStack());

    Hope it helps :)

    0 讨论(0)
  • 2020-11-28 04:38
    request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    

    This worked for me.

    0 讨论(0)
  • 2020-11-28 04:39

    I asked a similar question here:

    Android Volley makes 2 requests to the server when retry policy is set to 0

    I managed to solve this issue by setting the keep-alive property to false inside Android, e.g.:

    System.setProperty("http.keepAlive", "false")
    

    I added this line of code inside the class where I import requestqueue and make the requests.

    Also, check if you server has the keep-alive header.

    This post helped get to the solution.

    0 讨论(0)
提交回复
热议问题