com.android.volley
Class Request<T>

java.lang.Object
  extended by com.android.volley.Request<T>
Type Parameters:
T - The type of parsed response this request expects.
All Implemented Interfaces:
Comparable<Request<T>>
Direct Known Subclasses:
ClearCacheRequest, ImageRequest, JsonRequest, StringRequest

public abstract class Request<T>
extends Object
implements Comparable<Request<T>>

Base class for all network requests.


Nested Class Summary
static interface Request.Method
          Supported request methods.
static class Request.Priority
          Priority values.
 
Constructor Summary
Request(int method, String url, Response.ErrorListener listener)
          Creates a new request with the given method (one of the values from Request.Method), URL, and error listener.
Request(String url, Response.ErrorListener listener)
          Deprecated. Use Request(int, String, com.android.volley.Response.ErrorListener).
 
Method Summary
 void addMarker(String tag)
          Adds an event to this request's event log; for debugging.
 void cancel()
          Mark this request as canceled.
 int compareTo(Request<T> other)
          Our comparator sorts from high to low priority, and secondarily by sequence number to provide FIFO ordering.
 void deliverError(VolleyError error)
          Delivers error message to the ErrorListener that the Request was initialized with.
protected abstract  void deliverResponse(T response)
          Subclasses must implement this to perform delivery of the parsed response to their listeners.
(package private)  void finish(String tag)
          Notifies the request queue that this request has finished (successfully or with error).
 byte[] getBody()
          Returns the raw POST or PUT body to be sent.
 String getBodyContentType()
           
 Cache.Entry getCacheEntry()
          Returns the annotated cache entry, or null if there isn't one.
 String getCacheKey()
          Returns the cache key for this request.
 Map<String,String> getHeaders()
          Returns a list of extra HTTP headers to go along with this request.
 int getMethod()
          Return the method for this request.
protected  Map<String,String> getParams()
          Returns a Map of parameters to be used for a POST or PUT request.
protected  String getParamsEncoding()
          Returns which encoding should be used when converting POST or PUT parameters returned by getParams() into a raw POST or PUT body.
 byte[] getPostBody()
          Deprecated. Use getBody() instead.
 String getPostBodyContentType()
          Deprecated. Use getBodyContentType() instead.
protected  Map<String,String> getPostParams()
          Deprecated. Use getParams() instead.
protected  String getPostParamsEncoding()
          Deprecated. Use getParamsEncoding() instead.
 Request.Priority getPriority()
          Returns the Request.Priority of this request; Request.Priority.NORMAL by default.
 RetryPolicy getRetryPolicy()
          Returns the retry policy that should be used for this request.
 int getSequence()
          Returns the sequence number of this request.
 Object getTag()
          Returns this request's tag.
 int getTimeoutMs()
          Returns the socket timeout in milliseconds per retry attempt.
 int getTrafficStatsTag()
           
 String getUrl()
          Returns the URL of this request.
 boolean hasHadResponseDelivered()
          Returns true if this request has had a response delivered for it.
 boolean isCanceled()
          Returns true if this request has been canceled.
 void markDelivered()
          Mark this request as having a response delivered on it.
protected  VolleyError parseNetworkError(VolleyError volleyError)
          Subclasses can override this method to parse 'networkError' and return a more specific error.
protected abstract  Response<T> parseNetworkResponse(NetworkResponse response)
          Subclasses must implement this to parse the raw network response and return an appropriate response type.
 void setCacheEntry(Cache.Entry entry)
          Annotates this request with an entry retrieved for it from cache.
 void setRequestQueue(RequestQueue requestQueue)
          Associates this request with the given queue.
 void setRetryPolicy(RetryPolicy retryPolicy)
          Sets the retry policy for this request.
 void setSequence(int sequence)
          Sets the sequence number of this request.
 void setShouldCache(boolean shouldCache)
          Set whether or not responses to this request should be cached.
 void setTag(Object tag)
          Set a tag on this request.
 boolean shouldCache()
          Returns true if responses to this request should be cached.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Request

public Request(String url,
               Response.ErrorListener listener)
Deprecated. Use Request(int, String, com.android.volley.Response.ErrorListener).

Creates a new request with the given URL and error listener. Note that the normal response listener is not provided here as delivery of responses is provided by subclasses, who have a better idea of how to deliver an already-parsed response.


Request

public Request(int method,
               String url,
               Response.ErrorListener listener)
Creates a new request with the given method (one of the values from Request.Method), URL, and error listener. Note that the normal response listener is not provided here as delivery of responses is provided by subclasses, who have a better idea of how to deliver an already-parsed response.

Method Detail

getMethod

public int getMethod()
Return the method for this request. Can be one of the values in Request.Method.


setTag

public void setTag(Object tag)
Set a tag on this request. Can be used to cancel all requests with this tag by RequestQueue.cancelAll(Object).


getTag

public Object getTag()
Returns this request's tag.

See Also:
setTag(Object)

getTrafficStatsTag

public int getTrafficStatsTag()
Returns:
A tag for use with TrafficStats#setThreadStatsTag(int)

setRetryPolicy

public void setRetryPolicy(RetryPolicy retryPolicy)
Sets the retry policy for this request.


addMarker

public void addMarker(String tag)
Adds an event to this request's event log; for debugging.


finish

void finish(String tag)
Notifies the request queue that this request has finished (successfully or with error).

Also dumps all events from this request's event log; for debugging.


setRequestQueue

public void setRequestQueue(RequestQueue requestQueue)
Associates this request with the given queue. The request queue will be notified when this request has finished.


setSequence

public final void setSequence(int sequence)
Sets the sequence number of this request. Used by RequestQueue.


getSequence

public final int getSequence()
Returns the sequence number of this request.


getUrl

public String getUrl()
Returns the URL of this request.


getCacheKey

public String getCacheKey()
Returns the cache key for this request. By default, this is the URL.


setCacheEntry

public void setCacheEntry(Cache.Entry entry)
Annotates this request with an entry retrieved for it from cache. Used for cache coherency support.


getCacheEntry

public Cache.Entry getCacheEntry()
Returns the annotated cache entry, or null if there isn't one.


cancel

public void cancel()
Mark this request as canceled. No callback will be delivered.


isCanceled

public boolean isCanceled()
Returns true if this request has been canceled.


getHeaders

public Map<String,String> getHeaders()
                              throws AuthFailureError
Returns a list of extra HTTP headers to go along with this request. Can throw AuthFailureError as authentication may be required to provide these values.

Throws:
AuthFailureError - In the event of auth failure

getPostParams

protected Map<String,String> getPostParams()
                                    throws AuthFailureError
Deprecated. Use getParams() instead.

Returns a Map of POST parameters to be used for this request, or null if a simple GET should be used. Can throw AuthFailureError as authentication may be required to provide these values.

Note that only one of getPostParams() and getPostBody() can return a non-null value.

Throws:
AuthFailureError - In the event of auth failure

getPostParamsEncoding

protected String getPostParamsEncoding()
Deprecated. Use getParamsEncoding() instead.

Returns which encoding should be used when converting POST parameters returned by getPostParams() into a raw POST body.

This controls both encodings:

  1. The string encoding used when converting parameter names and values into bytes prior to URL encoding them.
  2. The string encoding used when converting the URL encoded parameters into a raw byte array.


getPostBodyContentType

public String getPostBodyContentType()
Deprecated. Use getBodyContentType() instead.


getPostBody

public byte[] getPostBody()
                   throws AuthFailureError
Deprecated. Use getBody() instead.

Returns the raw POST body to be sent.

Throws:
AuthFailureError - In the event of auth failure

getParams

protected Map<String,String> getParams()
                                throws AuthFailureError
Returns a Map of parameters to be used for a POST or PUT request. Can throw AuthFailureError as authentication may be required to provide these values.

Note that you can directly override getBody() for custom data.

Throws:
AuthFailureError - in the event of auth failure

getParamsEncoding

protected String getParamsEncoding()
Returns which encoding should be used when converting POST or PUT parameters returned by getParams() into a raw POST or PUT body.

This controls both encodings:

  1. The string encoding used when converting parameter names and values into bytes prior to URL encoding them.
  2. The string encoding used when converting the URL encoded parameters into a raw byte array.


getBodyContentType

public String getBodyContentType()

getBody

public byte[] getBody()
               throws AuthFailureError
Returns the raw POST or PUT body to be sent.

Throws:
AuthFailureError - in the event of auth failure

setShouldCache

public final void setShouldCache(boolean shouldCache)
Set whether or not responses to this request should be cached.


shouldCache

public final boolean shouldCache()
Returns true if responses to this request should be cached.


getPriority

public Request.Priority getPriority()
Returns the Request.Priority of this request; Request.Priority.NORMAL by default.


getTimeoutMs

public final int getTimeoutMs()
Returns the socket timeout in milliseconds per retry attempt. (This value can be changed per retry attempt if a backoff is specified via backoffTimeout()). If there are no retry attempts remaining, this will cause delivery of a TimeoutError error.


getRetryPolicy

public RetryPolicy getRetryPolicy()
Returns the retry policy that should be used for this request.


markDelivered

public void markDelivered()
Mark this request as having a response delivered on it. This can be used later in the request's lifetime for suppressing identical responses.


hasHadResponseDelivered

public boolean hasHadResponseDelivered()
Returns true if this request has had a response delivered for it.


parseNetworkResponse

protected abstract Response<T> parseNetworkResponse(NetworkResponse response)
Subclasses must implement this to parse the raw network response and return an appropriate response type. This method will be called from a worker thread. The response will not be delivered if you return null.

Parameters:
response - Response from the network
Returns:
The parsed response, or null in the case of an error

parseNetworkError

protected VolleyError parseNetworkError(VolleyError volleyError)
Subclasses can override this method to parse 'networkError' and return a more specific error.

The default implementation just returns the passed 'networkError'.

Parameters:
volleyError - the error retrieved from the network
Returns:
an NetworkError augmented with additional information

deliverResponse

protected abstract void deliverResponse(T response)
Subclasses must implement this to perform delivery of the parsed response to their listeners. The given response is guaranteed to be non-null; responses that fail to parse are not delivered.

Parameters:
response - The parsed response returned by parseNetworkResponse(NetworkResponse)

deliverError

public void deliverError(VolleyError error)
Delivers error message to the ErrorListener that the Request was initialized with.

Parameters:
error - Error details

compareTo

public int compareTo(Request<T> other)
Our comparator sorts from high to low priority, and secondarily by sequence number to provide FIFO ordering.

Specified by:
compareTo in interface Comparable<Request<T>>

toString

public String toString()
Overrides:
toString in class Object