Android Volley double post when have slow request
Asked Answered
B

18

65

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 resulting multiple (2 or more) postings for 1 request. I already set the retry policy to 0, but It doesn't help.

This is my LogCat

03-16 01:31:35.674: D/Volley(5984): [19807] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://[myserver]/api/places 0xfa7d0c33 NORMAL 1> [lifetime=3824], [size=313], [rc=200], [retryCount=0] 03-16 01:31:35.704: D/Volley(5984): [1] Request.finish: 3853 ms: [ ] http://[myserver]/api/places 0xfa7d0c33 NORMAL 1

This is my code

JSONObject body = new JSONObject();
try {
    body.put(PROTO_BODY_AUTHORIZATION, Sessions.getActiveSession().getToken());
} catch (JSONException e) {
    e.printStackTrace();
}

JsonObjectRequest request = new JsonObjectRequest(
        Request.Method.POST,
        context.getResources().getString(R.string.server_address) + "/places",
        body,
        callback,
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(context, error.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
);

request.setRetryPolicy(
        new DefaultRetryPolicy(
                DefaultRetryPolicy.DEFAULT_TIMEOUT_MS,
                0,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    getRequestQueue().add(request);

Please help, I desperately finding solution for this problem.

Bigwig answered 15/3, 2014 at 19:1 Comment(4)
Is your network request timeout the same amount of seconds as the retry policy timeout seconds?Habiliment
Where can I get the network request timeout amount? If I see from the LogCat, the request lifetime is 3825ms. The retry policy timeout amount is 2500ms (DefaultRetryPolicy.DEFAULT_TIMEOUT_MS)Bigwig
If I look at the source code of Volley, I see most Timeouts are set to 5000ms. I guess your post is double because your retry happens before the request timeout has passed, effectively sending it twice.Habiliment
Solved in this discussion #47937455Benedic
F
52

Add the following values to your Request object:

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

Here: request is your object of JsonObjectRequest. Change the value of multiplicator according to the DEFAULT TIMEOUT VALUE in DefaultRetryPolicy class in Volley.

You can also set the first argument to 0, like below:

request.setRetryPolicy(new DefaultRetryPolicy(
    0,
    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Fearsome answered 17/6, 2015 at 10:36 Comment(5)
still getting duplicates .. help ??Latten
You can set your default cache to falseFearsome
request.setRetryPolicy(new DefaultRetryPolicy(0,0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); Make: DefaultRetryPolicy.DEFAULT_MAX_RETRIES to 0Fearsome
your second answer setting the first value to 0, solved my issue.. thanks mate.Latten
That surely is not working for my code! Applied that solution for 3 months, but NOPE! doesn't work! I am still facing this issue as of Today!Sham
G
22

Just setting the Timeout in the RetryPolicy to 0 is too little. After checking the source, you have to actually set the maximum number of retries < 0, as it is checking current <= max...

I fixed double posting with setting the policy to the following

new DefaultRetryPolicy(0, -1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Galloway answered 12/3, 2015 at 16:40 Comment(1)
I believe this is the correct answer. Although you can set the timeout value tooCentillion
B
9

I found the solution for the double post, I just set the timeout to 0.

Bigwig answered 3/4, 2014 at 9:34 Comment(4)
Can you elaborate? Setting timeout to 0 should probably lead to more requests firing right?Thiamine
Apparently no, it seems 0 means no timeout. As I has discovered, if volley reaches timeout, it will fire another request. So setting timeout to 0 prevent volley from firing another request.Bigwig
I'm not too sure on this. I have just increased the timeout to like 30000 ms to just delay things for now.Thiamine
It's very bad solution, In my case splash activity send a post request to server and when user doesn't have internet so post request never failed and user is blocked, I think Droid_Mechanic answer is better.Doran
M
4

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));
Maes answered 11/3, 2015 at 15:36 Comment(0)
A
4

I found the solution for the multi post bug.

Change the RetryPolicy. I set the timeout value to 50000ms, worked fine Like this:

  request.setRetryPolicy(
                new DefaultRetryPolicy(
                        500000,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
                )
        );
Ax answered 1/7, 2015 at 3:53 Comment(0)
B
3

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 :)

Balaam answered 11/12, 2014 at 11:9 Comment(3)
How can we set connection.setChunkedStreamingMode(0); in openConnection method HurlStack class? I have tried to extend HurlStack but failed. I also tried your first solution but it's failed to.Squire
In your Volley library project, you can see HurlStack in toolbox package. And the first solution should work, try to experiment with different timeout values.Balaam
Yes, i did saw the HurlStack class but i can't find any way how to put connection.setChunkedStreamingMode(0);. Could you post some demo code? About the first solution, i tried all case, including change time out, change retry to 0 and System.setProperty("http.keepAlive", "false") but it still doesn't work.Squire
F
2

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.

Flyn answered 19/10, 2014 at 15:44 Comment(0)
N
2

please increase the setRetryPolicy time.

request.setRetryPolicy(new DefaultRetryPolicy(
                    30000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            Volley.newRequestQueue(this).add(equest);
Nihhi answered 6/7, 2016 at 11:44 Comment(0)
M
2

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;
}
Mcgurn answered 16/12, 2016 at 18:45 Comment(0)
C
2

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());
Characterize answered 2/6, 2017 at 5:0 Comment(0)
E
2

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));
Esperanzaespial answered 24/4, 2020 at 15:31 Comment(0)
U
1

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).

Unscrupulous answered 26/5, 2014 at 8:26 Comment(2)
Did you add the line in HurlStack.java file?Infuriate
Yes, I added it to openConnection() -method where the HttpURLConnection is being initialized.Unscrupulous
C
1

Best Solution:

jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(0, 
     DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

Worked for me.

Casaubon answered 16/10, 2019 at 13:43 Comment(0)
D
1

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

Demetrius answered 27/1, 2020 at 7:50 Comment(0)
C
0

just maxNumRetries = 0 no work. set TIMEOUT_MS 20000.

request.setRetryPolicy(new DefaultRetryPolicy(20000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Communize answered 21/4, 2015 at 1:57 Comment(1)
Can you explain what you mean here?Feathers
D
0
request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

This worked for me.

Dishonor answered 5/5, 2015 at 10:50 Comment(0)
K
0

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.

Kneehole answered 27/10, 2017 at 9:58 Comment(0)
C
0

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

Chihli answered 22/11, 2017 at 13:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.