Issue
I am trying to make a synchronous Volley networking request. I am using request futures to wait on a response, but the future.get()
call always times out (no matter how long the timeout is set to). I have tested every component individually and nothing seems to be causing the error other than my use of futures. Any ideas on what I've don wrong here?
Activity.java: persistData()
postCampaign(campaign);
Activity.java: postCampaign()
private boolean postCampaign(final Campaign c) {
RequestFuture<String> future = RequestFuture.newFuture();
StringRequest request = new StringRequest(Request.Method.POST, url, future, future) {
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<>();
// put data
return params;
}
};
NetworkController.getInstance(this).addToRequestQueue(request);
try {
String response = future.get(5, TimeUnit.SECONDS);
Log.d("Volley", "" + response);
return !response.contains("Duplicate");
} catch (InterruptedException|ExecutionException|TimeoutException e) {
Log.d("Volley", "[FAILED] " + e.getClass());
return false;
}
}
Strangely enough though, when stepping through the code, it appears that the RequestFuture's onResponse
method is invoked with the appropriate response. So it seems like the RequestFuture just isn't handling the response properly.
Solution
I think I've come to the conclusion that either Volley is not capable of fully synchronous networking requests or at the very least it isn't worth it. I ended up just showed a spinner on start and stopped it in the Volley request's onResponse
method when the server responded with a success message. As far as the user is concerned it works the same way.
Answered By - Caleb Whittington
Answer Checked By - David Marino (JavaFixing Volunteer)