T
- The type of parsed response this future expects.public class RequestFuture<T> extends Object implements Future<T>, Response.Listener<T>, Response.ErrorListener
RequestFuture<JSONObject> future = RequestFuture.newFuture(); MyRequest request = new MyRequest(URL, future, future); // If you want to be able to cancel the request: future.setRequest(requestQueue.add(request)); // Otherwise: requestQueue.add(request); try { JSONObject response = future.get(); // do something with response } catch (InterruptedException e) { // handle the error } catch (ExecutionException e) { // handle the error }
Modifier and Type | Method and Description |
---|---|
boolean |
cancel(boolean mayInterruptIfRunning)
Attempts to cancel execution of this task.
|
T |
get()
Waits if necessary for the computation to complete, and then
retrieves its result.
|
T |
get(long timeout,
TimeUnit unit)
Waits if necessary for at most the given time for the computation
to complete, and then retrieves its result, if available.
|
boolean |
isCancelled()
Returns
true if this task was cancelled before it completed
normally. |
boolean |
isDone()
Returns
true if this task completed. |
static <E> RequestFuture<E> |
newFuture() |
void |
onErrorResponse(VolleyError error)
Callback method that an error has been occurred with the
provided error code and optional user-readable message.
|
void |
onResponse(T response)
Called when a response is received.
|
void |
setRequest(Request<?> request) |
public static <E> RequestFuture<E> newFuture()
public void setRequest(Request<?> request)
public boolean cancel(boolean mayInterruptIfRunning)
Future
cancel
is called,
this task should never run. If the task has already started,
then the mayInterruptIfRunning
parameter determines
whether the thread executing this task should be interrupted in
an attempt to stop the task.
After this method returns, subsequent calls to Future.isDone()
will
always return true
. Subsequent calls to Future.isCancelled()
will always return true
if this method returned true
.
cancel
in interface Future<T>
mayInterruptIfRunning
- true
if the thread executing this
task should be interrupted; otherwise, in-progress tasks are allowed
to completefalse
if the task could not be cancelled,
typically because it has already completed normally;
true
otherwisepublic T get() throws InterruptedException, ExecutionException
Future
get
in interface Future<T>
InterruptedException
- if the current thread was interrupted
while waitingExecutionException
- if the computation threw an
exceptionpublic T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
Future
get
in interface Future<T>
timeout
- the maximum time to waitunit
- the time unit of the timeout argumentInterruptedException
- if the current thread was interrupted
while waitingExecutionException
- if the computation threw an
exceptionTimeoutException
- if the wait timed outpublic boolean isCancelled()
Future
true
if this task was cancelled before it completed
normally.isCancelled
in interface Future<T>
true
if this task was cancelled before it completedpublic boolean isDone()
Future
true
if this task completed.
Completion may be due to normal termination, an exception, or
cancellation -- in all of these cases, this method will return
true
.public void onResponse(T response)
Response.Listener
onResponse
in interface Response.Listener<T>
public void onErrorResponse(VolleyError error)
Response.ErrorListener
onErrorResponse
in interface Response.ErrorListener