Dharmin Shah
Dharmin Shah

Reputation: 41

how to make volley request should not retry any way after calling first time?

I'm making request to call server using volley, But sometimes when there is latency in network it again makes request to the server.

But as per my request it should call server to only one time irrespective of response from server.

Upvotes: 2

Views: 1219

Answers (2)

LutfiTekin
LutfiTekin

Reputation: 471

public RetryPolicy myRetryPolicy(){
  return new RetryPolicy() {
      @Override
      public int getCurrentTimeout() {
          return 10000;
      }

      @Override
      public int getCurrentRetryCount() {
          return 0;
      }

      @Override
      public void retry(VolleyError error) throws VolleyError {
        Log.d(TAG, "Volley Error " + error.toString());
          throw new VolleyError("Do Not Retry");
      }
  };
}

If you set a custom RetryPolicy and in that, throw a VolleyError in the retry method. Volley never retries.

postRequest.setRetryPolicy(myRetryPolicy());

LogCat

11-05 13:00:36.078 6014-6314/******: Volley Error com.android.volley.TimeoutError 11-05 13:00:36.079 6014-6014/*******: Volley Error com.android.volley.VolleyError: Do Not Retry

Upvotes: 1

Kartheek
Kartheek

Reputation: 7214

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

Upvotes: 1

Related Questions