So, I have this 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 send a very large JSON
, so the default timeout of 5 seconds is not enough. So, I tried to increase the timeout to 30 seconds and messing with the DefaultRetryPolicy
to increase the number of retries.
The thing is, it keeps timeouting
in 5s and it doesn't even retry once!
Do I have to have a listener or a callback for the retries ? I'm doing something wrong with the DefaultRetryPolicy
? Please help, this issue is driving me nuts...
Do you need to use the DefaultRetryPolicy?
Because you could define your own.
Instead of this:
Try this:
I'm not sure exactly why the retry time doesn't work on your code, I did find a similar issue here though.
Instead, I can tell you some things that I don't believe to be ok in your code and suggest you to adopt my model of Volley use.
First of all, you're creating a new request queue for every request you're making. That's not cool, you should have a RequestManager singleton that holds one request queue and use that.
Second of all, I don't know if this is what affects the retry time, I have a base request class and set the retry time in the constructor. Then, I extend this class whenever I have to implement a new type of request. Then, I create an instance of the request, set the callbacks, and pass it to the request manager. The request manager adds it to the one request queue I was talking about.
More over, if you don't already, I suggest you use the Gson library to parse the JSON objects.
This is my base request class I'm using:
This works like a charm for me. Hope it helps, if you need any further help, contact me