Self-contained tutorial for RequestFuture

273 views
Skip to first unread message

list...@gmail.com

unread,
Dec 17, 2015, 3:31:39 PM12/17/15
to Volley Users
Hello everyone,

I have gone through multiple StackOverflow questions, blog posts on how to use Volley's RequestFuture.
The ones I found all have these lines in common:

RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest request = new JsonObjectRequest(URL, null, future, future);
requestQueue.add(request);
 try {
        return future.get(30, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            // exception handling
        } catch (ExecutionException e) {
            // exception handling
        } catch (TimeoutException e) {
            // exception handling
}
I do that in my MainActivity that is created using Android Studio's template project.
As you may guess, this doesn't work since it times out. According to this SO question, I am not supposed to do this in the main thread.
Indeed I can put a breakpoint and see that I get a response from server but that response seems to be happening on a different thread and never gets delivered to future.get() line.

I am new to Android and cannot figure out how I am supposed to properly use future.get()
so I was wondering if anyone is aware of a self-contained example that showcases proper use of RequestFuture on a different thread.

My use case is that user clicks a tab and I want to to wait until all data (for example a city's daily temperature data) is fetched before I show the tab that will display the data in a nice chart/graph.

I have put a sample code that I am trying to make work on github: https://github.com/spamwax/VolleySync

Thanks in advance,

Ficus Kirkpatrick

unread,
Dec 17, 2015, 3:42:24 PM12/17/15
to list...@gmail.com, Volley Users
Why are you trying to use RequestFuture if you want to do something from your Activity? Most of the point of Volley is to be able to dispatch from main thread and receive a callback on the main thread and live the async life.

--
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.

list...@gmail.com

unread,
Dec 17, 2015, 3:55:24 PM12/17/15
to Volley Users, list...@gmail.com
Ficus, thanks for your reply.

I am using Volley as a way of making http calls to a backend API server.
I am trying to make sure the specific tab will be only displayed if I receive the data and process it first in order to generate the charts.
While the data is being fetched/processed I want to block (show ProgressBar?) so the user doesn't see an empty view.
As I said I am new to android so I am guessing there is a better paradigm to do this?

Maybe the snippet is not a good example of doing this, I just wanted to create a simple app in order to figure out this requirement.

Any input on how to do this is greatly appreciated.

Ficus Kirkpatrick

unread,
Dec 17, 2015, 9:00:11 PM12/17/15
to list...@gmail.com, Volley Users
The typical approach is to show a loading screen of some kind, add your HTTP request to the RequestQueue, and in your Listener, hide the loading UI and show the UI that will contain the data you've received from your server (e.g. ListView, RecyclerView, so on)

list...@gmail.com

unread,
Dec 18, 2015, 1:55:39 AM12/18/15
to Volley Users, list...@gmail.com
Thanks for the suggestion, I'll follow it.
Just being curious, what is a good use case example for future.get() if listeners can mostly do the job? What's the main purpose of having them in the library?

Ficus Kirkpatrick

unread,
Dec 18, 2015, 10:19:42 AM12/18/15
to list...@gmail.com, Volley Users

Futures are for people who want to use Volley to make blocking calls on worker threads. They aren't used by most people.


Reply all
Reply to author
Forward
0 new messages