Blocking Request using RequestFuture never returns and eventually runs into TimeoutException

4,028 views
Skip to first unread message

Hubert Baretta

unread,
May 23, 2014, 6:34:31 AM5/23/14
to volley...@googlegroups.com
I'm trying to make a blocking/synchronous http request using volley, basically based on the example in the com.android.volley.toolbox.RequestFuture class:       

[...]
RequestFuture<String> future = RequestFuture.newFuture();
 MyRequest request = new MyRequest(URL, future, errorListener);
requestQueue.add(request);
try {
     String response = future.get(10, TimeUnit.SECONDS);
} catch (InterruptedException e)
{ // handle the error }
catch (ExecutionException e)
{ // handle the error }
catch (TimeoutException e)
{ // handle the error }
[...]

Whatever I tried, the future.get call never actually received the result.
I believe this might be since the RequestFuture makes the current (main) thread wait which seemingly also stops the response processing of volley. On the other hand I am basically using the code from the volley example and since volley states that requests are always executed on a different thread, I believe this should not be happening. Only after the timeout the processing continues and Request.finish is logged for the request.
I tried changing the http stack and also replacing com.android.volley.toolbox.RequestFuture by my own Future implementation with a countdownlatch, but neither method allowed the response to be received in the future.get call.
Any ideas what I'm doing wrong?

Ficus Kirkpatrick

unread,
May 23, 2014, 10:17:43 AM5/23/14
to Hubert Baretta, volley...@googlegroups.com

Are you calling get() from the main thread?

--
You received this message because you are subscribed to the Google Groups "Volley Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to volley-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Evan Charlton

unread,
May 23, 2014, 10:48:26 AM5/23/14
to Ficus Kirkpatrick, Hubert Baretta, volley...@googlegroups.com
To add on to what Ficus said (don't block the main thread!), you should also pass the future in for the ErrorListener, too. Otherwise, if an error occurs, your future won't return (until the timeout).

Evan

Hubert Baretta

unread,
May 23, 2014, 11:24:06 AM5/23/14
to volley...@googlegroups.com, Ficus Kirkpatrick, Hubert Baretta
Thank you for the hint, but for testing purposes I had the errorListener just throw an exception, so I would have noticed if an error had occured. (and I also had used the future as errorlistener in a previous attempt).

Hubert Baretta

unread,
May 23, 2014, 11:36:24 AM5/23/14
to volley...@googlegroups.com, Hubert Baretta
Yes, I'm calling it from the main thread.

My intention was to have a reusable login method which I can use to execute the request and then return back to the current execution stack without having to create some boilerplate callback like a ResponseListener first. Since in Java I can't pass the calling method as a parameter in a functional way, I did not come up with a better solution than using a future (and apparently blocking the main thread which Volley seems to need for receiving responses). Starting it in a different thread would also require me to create some kind of callback to return to the main thread when the response is received...

Any ideas how to generically start a login request before another request is executed without the hassle to create a callback from wherever it is used?

Ficus Kirkpatrick

unread,
May 23, 2014, 12:07:18 PM5/23/14
to Hubert Baretta, volley...@googlegroups.com
As Evan said, you should never block on the main thread in any circumstance.

Two ways to do login:

1) Use the dreaded callback, and then kick off the next request from onResponse.

2) Write your own Request, or subclass one from the toolbox. Say you're passing an auth token via a header. You can block to your heart's content in getHeaders() as it is guaranteed to be called from a worker thread.

Ficus

Reply all
Reply to author
Forward
0 new messages