Android Volley double post when have slow request

后端 未结 18 2687
生来不讨喜
生来不讨喜 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:44

    This works for me.

    public class MyRetryPolicyWithoutRetry implements RetryPolicy
    {
        @override
        public int getCurrentTimeout()
        {
            return CONNECTION_TIME_OUT; /200000/
        }
    
        @Override
        public int getCurrentRetryCount()
        {
            return 0;
        }
    
        @Override
        public void retry(VolleyError error) throws VolleyError
        {
            throw(error);
        }
    }
    

    To use:

    request.setRetryPolicy(new MyRetryPolicyWithoutRetry());
    
    0 讨论(0)
  • 2020-11-28 04:49

    Tried lot of things but in the end nothing helped. Finally, I figured out following combination of changes:

    sr.setRetryPolicy(new DefaultRetryPolicy(0,-1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    

    And in your hurl connection in application class, add this:

    httpsURLConnection.setChunkedStreamingMode(0);
    

    This worked smoothly to stop Volley hitting multiple request at server.

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

    The only way I got the double requests to stop was to set the retry policy retries to -1

    request.setRetryPolicy(new DefaultRetryPolicy(0, -1, 0));
    

    I think this is because the DefaultRetryPolicy's logic for attempts remaining returns true if retryCount is 0 and Max retries is also 0 in the hasAttemptRemaining() method:

    protected boolean hasAttemptRemaining() {
        return this.mCurrentRetryCount <= this.mMaxNumRetries;
    }
    
    0 讨论(0)
  • 2020-11-28 04:52

    correcting the android date fixes my problem unfortunately for testing purpose i have changed my android date and get ssl security error.

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

    I managed to fix this by configuring HttpURLConnection like this:

    connection.setChunkedStreamingMode(0);
    

    I started a discussion about this in Volley email list (https://groups.google.com/forum/#!topic/volley-users/8PE9dBbD6iA).

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

    You must set the RetryPolicy to 0 retries and ensure that the timeout is bigger than the server timeout.

    setRetryPolicy(new DefaultRetryPolicy("bigger than server timeout",
                                          0,
                                          DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    
    0 讨论(0)
提交回复
热议问题