Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

android - How to prevent Volley request from retrying?

I post a JsonRequest to a server. The server is slow and Volley tends to make multiple calls to the slow server because it didn't get a response from the first request (since my server is slow). Is there a way to prevent Volley from retrying a request so that it can receive the first response?

I have tried:

myRequest.setRetryPolicy(new DefaultRetryPolicy(
                TIMEOUT_MS, 
                RETRIES, 
                BACKOFF_MULT)); 

I have replaced TIMEOUT_MS with 0, RETRIES with 0, and also BACKOFF_MULT with 0, but it didn't work.

Any suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Volley Default Retry Policy is:

/** The default socket timeout in milliseconds */
public static final int DEFAULT_TIMEOUT_MS = 2500;

/** The default number of retries */
public static final int DEFAULT_MAX_RETRIES = 1;

/** The default backoff multiplier */
public static final float DEFAULT_BACKOFF_MULT = 1f;

You can find it in DefaultRetryPolicy.java,

so you can see that volley makes 1 retry request by default.

Try to use smaller TIMEOUT (if you don't want to wait the 2500ms), or bigger than 2500ms to get the answer), but keep the other values, for example:

// Wait 20 seconds and don't retry more than once
myRequest.setRetryPolicy(new DefaultRetryPolicy(
       (int) TimeUnit.SECONDS.toMillis(20),
       DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
       DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

Hence, to disable Volley from retrying you will need to do:

myRequest.setRetryPolicy(new DefaultRetryPolicy(
       (int) TimeUnit.SECONDS.toMillis(20), //After the set time elapses the request will timeout
       0,
       DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...