Android volley DefaultRetryPolicy not working properly

So, I have a Volley PUT request:

private boolean syncCall(JSONObject jsonObject, final VolleyCallback callback) { final ProgressDialog progDailog = new ProgressDialog(context); final Boolean[] success = {false}; progDailog.setMessage("..."); progDailog.setIndeterminate(false); progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progDailog.setCancelable(false); progDailog.show(); final SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(context); RequestQueue queue = Volley.newRequestQueue(context, new HurlStack()); final String token = prefs.getString("token", null); String URL = Constants.getUrlSync(); String param1 = String.valueOf(prefs.getInt("pmp", 1)); String param2 = String.valueOf(prefs.getInt("ei", 1)); URL = URL.replace("[x]", param1); URL = URL.replace("[y]", param2); //pegar id pmp e IE corretas JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request .Method.PUT, URL, jsonObject, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { callback.onSuccess(response + ""); success[0] = true; progDailog.dismiss(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { callback.onFailure(error); tokenFailure(error); success[0] = false; progDailog.dismiss(); } }) { @Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<>(); headers.put("Token", token); return headers; } }; int socketTimeout = 30000; RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); jsObjRequest.setRetryPolicy(policy); queue.add(jsObjRequest); return success[0]; } 

My problem is that I am sending very large JSON , so the default is not enough 5 seconds. So, I tried to increase the timeout to 30 seconds and mess with DefaultRetryPolicy to increase the number of attempts.

The thing is, it stores timeouting in 5s, and it doesn't even repeat once!

Should I have a listener or callback for retries? Am I doing something wrong with DefaultRetryPolicy ? Please help, this problem is driving me crazy ...

+6
source share
2 answers

Do you need to use DefaultRetryPolicy?

Because you can define your own.

Instead of this:

 RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); 

Try the following:

 jsObjRequest.setRetryPolicy(new RetryPolicy() { @Override public int getCurrentTimeout() { // Here goes the new timeout return mySeconds; } @Override public int getCurrentRetryCount() { // The max number of attempts return myAttempts; } @Override public void retry(VolleyError error) throws VolleyError { // Here you could check if the retry count has gotten // To the max number, and if so, send a VolleyError msg // or something } }); 
+5
source

I don’t know exactly why the retry time doesn’t work with your code, however I found a similar problem here .

Instead, I can tell you something that, it seems to me, is not suitable for your code, and invite you to accept my Volley usage model.

First of all, you create a new request queue for each request. This is not cool, you should have a RequestManager singleton containing one request queue, and use it.

Secondly, I do not know if this affects the retry time, I have a base request class and I set the retry time in the constructor. Then I extend this class whenever I need to implement a new request type. Then I instantiate the request, set callbacks and pass it to the query manager. The query manager adds it to one query queue, which I talked about.

Moreover, if you have not done so already, I suggest you use the Gson library to parse JSON objects.

This is my base query class that I use:

 /** * Created by Daniel on 2/6/2016. */ public class GsonRequest<T> extends Request<T> { protected Context context; protected final Gson gson = new Gson(); protected final Class<T> clazz; protected final TypeToken typeToken; protected Map<String, String> headers; protected Map<String, String> params; protected final Response.Listener<T> listener; /** * Make a GET request and return a parsed object from JSON. * * @param url URL of the request to make * @param clazz Relevant class object, for Gson reflection */ public GsonRequest(final Context context, final int requestMethod, String url, Class<T> clazz, Response.Listener<T> listener, Response.ErrorListener errorListener) { super(requestMethod, url, errorListener); this.context = context; this.clazz = clazz; this.listener = listener; this.headers = new HashMap<>(); typeToken = null; setRetryPolicy(); } /** * Make a GET request and return a parsed object from JSON. * * @param url URL of the request to make * @param typeToken Relevant typeToken object, for Gson reflection */ public GsonRequest(final Context context, final int requestMethod, String url, TypeToken typeToken, Response.Listener<T> listener, Response.ErrorListener errorListener) { super(requestMethod, url, errorListener); this.context = context; this.typeToken = typeToken; this.listener = listener; this.headers = new HashMap<>(); clazz = null; setRetryPolicy(); } @Override protected Map<String, String> getParams() throws AuthFailureError { return params != null ? params : super.getParams(); } @Override public Map<String, String> getHeaders() throws AuthFailureError { //TODO add headers here return headers; } @Override protected void deliverResponse(T response) { listener.onResponse(response); } @Override protected Response<T> parseNetworkResponse(NetworkResponse response) { try { String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); JSONObject jsonObject = new JSONObject(json); if (clazz != null) { return Response.success(gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response)); } else { return Response.success((T) gson.fromJson(json, typeToken.getType()), HttpHeaderParser.parseCacheHeaders(response)); } } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JsonSyntaxException e) { return Response.error(new ParseError(e)); } catch (JSONException e) { return Response.error(new ParseError(e)); } } protected void setRetryPolicy() { //TODO set your retry policy here setRetryPolicy(new DefaultRetryPolicy( 30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); } }'enter code here' 

It works like a charm for me. Hope this helps, if you need more help contact me

+1
source

All Articles