How To Prevent Volley Request From Retrying?
Solution 1:
The Volley Default Retry Policy is:
/** The default socket timeout in milliseconds */publicstaticfinalint DEFAULT_TIMEOUT_MS = 2500;
/** The default number of retries */publicstaticfinalint DEFAULT_MAX_RETRIES = 1;
/** The default backoff multiplier */publicstaticfinalfloat 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(newDefaultRetryPolicy(
(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(newDefaultRetryPolicy(
(int) TimeUnit.SECONDS.toMillis(20), //After the set time elapses the request will timeout0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Solution 2:
Try this,
// remove caching
jsObjRequest.setShouldCache(false);
// Wait 30 seconds and don't retry more than once
jsObjRequest.setRetryPolicy(newDefaultRetryPolicy(0, 0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Method call form task
publicvoidcallWebService(JSONObject jsonRequest) {
JsonObjectRequest jsObjRequest = newJsonObjectRequest(
Request.Method.POST, url + pageurl, jsonRequest,
newResponse.Listener<JSONObject>() {
@OverridepublicvoidonResponse(JSONObject jsonObject) {
sendResponse(jsonObject);
}
}, newResponse.ErrorListener() {
@OverridepublicvoidonErrorResponse(VolleyError error) {
callBack.onError(error);
try {
if (progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
} catch (Exception e) {
Helper.customPrintStackTrace(e);
}
}
}
) {
@OverridepublicStringgetBodyContentType() {
return"application/json; charset=utf-8";
}
@OverridepublicMap<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = newHashMap<String, String>();
params.put(WebConfig.WebParams.APIKEY, Helper
.getStringValuefromPrefs(context,
SharedPreferencesKey.PREF_APIKEY));
params.put(WebConfig.WebParams.APITOKEN, Helper
.getStringValuefromPrefs(context,
SharedPreferencesKey.PREF_APITOKEN));
if (!Helper.getStringValuefromPrefs(context,
SharedPreferencesKey.PREF_USERID).equals("")) {
params.put(WebConfig.WebParams.USER_ID, Helper
.getStringValuefromPrefs(context,
SharedPreferencesKey.PREF_USERID));
} else {
params.put(WebConfig.WebParams.USER_ID, "0");
}
return params;
}
};
// remove caching
jsObjRequest.setShouldCache(false);
// Wait 30 seconds and don't retry more than once
jsObjRequest.setRetryPolicy(newDefaultRetryPolicy(0, 0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Access the RequestQueue through your singleton class.VolleySingleton.getInstance(context).addToRequestQueue(jsObjRequest);
if (showDefultProcess) {
progressDialog.show();
}
}
Solution 3:
To stop Volley from retrying a request, simply set the retry policy of the request to a DefaultRetryPolicy
with maxNumRetries
being 0:
myRequest.setRetryPolicy(newDefaultRetryPolicy(
DefaultRetryPolicy.DEFAULT_TIMEOUT_MS,
0, // maxNumRetries = 0 means no retry
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Solution 4:
Using your StringRequest Object or JsonObjectRequest Object or JsonArrayRequest Object call this method.
For example if the object is an instance of StringRequest, here is the method:
stringRequest.setRetryPolicy(newDefaultRetryPolicy(initialTimeoutMs, maxNumRetries,
backoffMultiplier ));
initialTimeoutMs The initial timeout for the policy.
maxNumRetries The maximum number of retries.
backoffMultiplier Backoff multiplier for the policy.
Below are the parameters which I gave.
stringRequest.setRetryPolicy(newDefaultRetryPolicy(10 * 1000, 0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
First parameter says set InitialTimeout to 10 seconds.
Second parameter says set retry count tries to 0.
Third parameter is default.
Post a Comment for "How To Prevent Volley Request From Retrying?"