public class GraphRequest
extends java.lang.Object
A single request to be sent to the Facebook Platform through the Graph API. The Request class provides functionality relating to serializing and deserializing requests and responses, making calls in batches (with a single round-trip to the service) and making calls asynchronously.
The particular service endpoint that a request targets is determined by a graph path (see the
setGraphPath
method).
A Request can be executed either anonymously or representing an authenticated user. In the former
case, no AccessToken needs to be specified, while in the latter, an AccessToken must be provided.
If requests are executed in a batch, a Facebook application ID must be associated with the batch,
either by setting the application ID in the AndroidManifest.xml or via FacebookSdk or by calling
the setDefaultBatchApplicationId
method.
After completion of a request, the AccessToken, if not null and taken from AccessTokenManager, will be checked to determine if its Facebook access token needs to be extended; if so, a request to extend it will be issued in the background.
Modifier and Type | Class and Description |
---|---|
static interface |
GraphRequest.Callback
Specifies the interface that consumers of the Request class can implement in order to be
notified when a particular request completes, either successfully or with an error.
|
static interface |
GraphRequest.GraphJSONArrayCallback
Callback for requests that result in an array of JSONObjects.
|
static interface |
GraphRequest.GraphJSONObjectCallback
Callback for requests that result in a JSONObject.
|
static interface |
GraphRequest.OnProgressCallback
Specifies the interface that consumers of the Request class can implement in order to be
notified when a progress is made on a particular request.
|
static class |
GraphRequest.ParcelableResourceWithMimeType<RESOURCE extends android.os.Parcelable>
Used during serialization for the graph request.
|
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
ACCESS_TOKEN_PARAM |
static java.lang.String |
FIELDS_PARAM |
static int |
MAXIMUM_BATCH_SIZE
The maximum number of requests that can be submitted in a single batch.
|
static java.lang.String |
TAG |
Constructor and Description |
---|
GraphRequest()
Constructs a request without an access token, graph path, or any other parameters.
|
GraphRequest(AccessToken accessToken,
java.lang.String graphPath)
Constructs a request with an access token to retrieve a particular graph path.
|
GraphRequest(AccessToken accessToken,
java.lang.String graphPath,
android.os.Bundle parameters,
HttpMethod httpMethod)
Constructs a request with a specific AccessToken, graph path, parameters, and HTTP method.
|
GraphRequest(AccessToken accessToken,
java.lang.String graphPath,
android.os.Bundle parameters,
HttpMethod httpMethod,
GraphRequest.Callback callback)
Constructs a request with a specific access token, graph path, parameters, and HTTP method.
|
GraphRequest(AccessToken accessToken,
java.lang.String graphPath,
android.os.Bundle parameters,
HttpMethod httpMethod,
GraphRequest.Callback callback,
java.lang.String version)
Constructs a request with a specific access token, graph path, parameters, and HTTP method.
|
Modifier and Type | Method and Description |
---|---|
GraphResponse |
executeAndWait()
Executes this request on the current thread and blocks while waiting for the response.
|
static GraphResponse |
executeAndWait(GraphRequest request)
Executes a single request on the current thread and blocks while waiting for the response.
|
GraphRequestAsyncTask |
executeAsync()
Executes the request asynchronously.
|
static java.util.List<GraphResponse> |
executeBatchAndWait(java.util.Collection<GraphRequest> requests)
Executes requests as a single batch on the current thread and blocks while waiting for the
responses.
|
static java.util.List<GraphResponse> |
executeBatchAndWait(GraphRequest... requests)
Executes requests on the current thread as a single batch and blocks while waiting for the
response.
|
static java.util.List<GraphResponse> |
executeBatchAndWait(GraphRequestBatch requests)
Executes requests on the current thread as a single batch and blocks while waiting for the
responses.
|
static GraphRequestAsyncTask |
executeBatchAsync(java.util.Collection<GraphRequest> requests)
Executes requests as a single batch asynchronously.
|
static GraphRequestAsyncTask |
executeBatchAsync(GraphRequest... requests)
Executes requests as a single batch asynchronously.
|
static GraphRequestAsyncTask |
executeBatchAsync(GraphRequestBatch requests)
Executes requests as a single batch asynchronously.
|
static java.util.List<GraphResponse> |
executeConnectionAndWait(java.net.HttpURLConnection connection,
java.util.Collection<GraphRequest> requests)
Executes requests that have already been serialized into an HttpURLConnection.
|
static java.util.List<GraphResponse> |
executeConnectionAndWait(java.net.HttpURLConnection connection,
GraphRequestBatch requests)
Executes requests that have already been serialized into an HttpURLConnection.
|
static GraphRequestAsyncTask |
executeConnectionAsync(android.os.Handler callbackHandler,
java.net.HttpURLConnection connection,
GraphRequestBatch requests)
Asynchronously executes requests that have already been serialized into an HttpURLConnection.
|
static GraphRequestAsyncTask |
executeConnectionAsync(java.net.HttpURLConnection connection,
GraphRequestBatch requests)
Asynchronously executes requests that have already been serialized into an HttpURLConnection.
|
AccessToken |
getAccessToken()
Returns the access token associated with this request.
|
java.lang.String |
getBatchEntryDependsOn()
Returns the name of the request that this request entry explicitly depends on in a batched
request.
|
java.lang.String |
getBatchEntryName()
Returns the name of this requests entry in a batched request.
|
boolean |
getBatchEntryOmitResultOnSuccess()
Returns whether or not this batch entry will return a response if it is successful.
|
GraphRequest.Callback |
getCallback()
Returns the callback which will be called when the request finishes.
|
static java.lang.String |
getDefaultBatchApplicationId()
Gets the default Facebook application ID that will be used to submit batched requests.
|
org.json.JSONObject |
getGraphObject()
Returns the GraphObject, if any, associated with this request.
|
java.lang.String |
getGraphPath()
Returns the graph path of this request, if any.
|
HttpMethod |
getHttpMethod()
Returns the
HttpMethod to use for this request. |
android.os.Bundle |
getParameters()
Returns the parameters for this request.
|
java.lang.Object |
getTag()
Gets the tag on the request; this is an application-defined object that can be used to
distinguish between different requests.
|
java.lang.String |
getVersion()
Returns the version of the API that this request will use.
|
static GraphRequest |
newCustomAudienceThirdPartyIdRequest(AccessToken accessToken,
android.content.Context context,
GraphRequest.Callback callback)
Creates a new Request configured to retrieve an App User ID for the app's Facebook user.
|
static GraphRequest |
newCustomAudienceThirdPartyIdRequest(AccessToken accessToken,
android.content.Context context,
java.lang.String applicationId,
GraphRequest.Callback callback)
Creates a new Request configured to retrieve an App User ID for the app's Facebook user.
|
static GraphRequest |
newDeleteObjectRequest(AccessToken accessToken,
java.lang.String id,
GraphRequest.Callback callback)
Creates a new Request configured to delete a resource through the Graph API.
|
static GraphRequest |
newGraphPathRequest(AccessToken accessToken,
java.lang.String graphPath,
GraphRequest.Callback callback)
Creates a new Request configured to retrieve a particular graph path.
|
static GraphRequest |
newMeRequest(AccessToken accessToken,
GraphRequest.GraphJSONObjectCallback callback)
Creates a new Request configured to retrieve a user's own profile.
|
static GraphRequest |
newMyFriendsRequest(AccessToken accessToken,
GraphRequest.GraphJSONArrayCallback callback)
Creates a new Request configured to retrieve a user's friend list.
|
static GraphRequest |
newPlacesSearchRequest(AccessToken accessToken,
android.location.Location location,
int radiusInMeters,
int resultsLimit,
java.lang.String searchText,
GraphRequest.GraphJSONArrayCallback callback)
Creates a new Request that is configured to perform a search for places near a specified
location via the Graph API.
|
static GraphRequest |
newPostRequest(AccessToken accessToken,
java.lang.String graphPath,
org.json.JSONObject graphObject,
GraphRequest.Callback callback)
Creates a new Request configured to post a GraphObject to a particular graph path, to either
create or update the object at that path.
|
static GraphRequest |
newUploadPhotoRequest(AccessToken accessToken,
java.lang.String graphPath,
android.graphics.Bitmap image,
java.lang.String caption,
android.os.Bundle params,
GraphRequest.Callback callback)
Creates a new Request configured to upload a photo to the specified graph path.
|
static GraphRequest |
newUploadPhotoRequest(AccessToken accessToken,
java.lang.String graphPath,
java.io.File file,
java.lang.String caption,
android.os.Bundle params,
GraphRequest.Callback callback)
Creates a new Request configured to upload a photo to the specified graph path.
|
static GraphRequest |
newUploadPhotoRequest(AccessToken accessToken,
java.lang.String graphPath,
android.net.Uri photoUri,
java.lang.String caption,
android.os.Bundle params,
GraphRequest.Callback callback)
Creates a new Request configured to upload a photo to the specified graph path.
|
void |
setAccessToken(AccessToken accessToken)
Sets the access token to use for this request.
|
void |
setBatchEntryDependsOn(java.lang.String batchEntryDependsOn)
Sets the name of the request entry that this request explicitly depends on in a batched
request.
|
void |
setBatchEntryName(java.lang.String batchEntryName)
Sets the name of this request's entry in a batched request.
|
void |
setBatchEntryOmitResultOnSuccess(boolean batchEntryOmitResultOnSuccess)
Sets whether or not this batch entry will return a response if it is successful.
|
void |
setCallback(GraphRequest.Callback callback)
Sets the callback which will be called when the request finishes.
|
static void |
setDefaultBatchApplicationId(java.lang.String applicationId)
Sets the default application ID that will be used to submit batched requests if none of those
requests specifies an access token.
|
void |
setGraphObject(org.json.JSONObject graphObject)
Sets the GraphObject associated with this request.
|
void |
setGraphPath(java.lang.String graphPath)
Sets the graph path of this request.
|
void |
setHttpMethod(HttpMethod httpMethod)
Sets the
HttpMethod to use for this request. |
void |
setParameters(android.os.Bundle parameters)
Sets the parameters for this request.
|
void |
setSkipClientToken(boolean skipClientToken)
This is an internal function that is not meant to be used by developers.
|
void |
setTag(java.lang.Object tag)
Sets the tag on the request; this is an application-defined object that can be used to
distinguish between different requests.
|
void |
setVersion(java.lang.String version)
Set the version to use for this request.
|
static java.net.HttpURLConnection |
toHttpConnection(java.util.Collection<GraphRequest> requests)
Serializes one or more requests but does not execute them.
|
static java.net.HttpURLConnection |
toHttpConnection(GraphRequest... requests)
Serializes one or more requests but does not execute them.
|
static java.net.HttpURLConnection |
toHttpConnection(GraphRequestBatch requests)
Serializes one or more requests but does not execute them.
|
java.lang.String |
toString()
Returns a string representation of this Request, useful for debugging.
|
public static final int MAXIMUM_BATCH_SIZE
public static final java.lang.String TAG
public static final java.lang.String ACCESS_TOKEN_PARAM
public static final java.lang.String FIELDS_PARAM
public GraphRequest()
public GraphRequest(@Nullable AccessToken accessToken, java.lang.String graphPath)
accessToken
- the access token to use, or nullgraphPath
- the graph path to retrievepublic GraphRequest(@Nullable AccessToken accessToken, java.lang.String graphPath, android.os.Bundle parameters, HttpMethod httpMethod)
accessToken
- the access token to use, or nullgraphPath
- the graph path to retrieve, create, or deleteparameters
- additional parameters to pass along with the Graph API request; parameters
must be Strings, Numbers, Bitmaps, Dates, or Byte arrays.httpMethod
- the HttpMethod
to use for the request, or null for default
(HttpMethod.GET)public GraphRequest(@Nullable AccessToken accessToken, java.lang.String graphPath, android.os.Bundle parameters, HttpMethod httpMethod, GraphRequest.Callback callback)
accessToken
- the access token to use, or nullgraphPath
- the graph path to retrieve, create, or deleteparameters
- additional parameters to pass along with the Graph API request; parameters
must be Strings, Numbers, Bitmaps, Dates, or Byte arrays.httpMethod
- the HttpMethod
to use for the request, or null for default
(HttpMethod.GET)callback
- a callback that will be called when the request is completed to handle
success or error conditionspublic GraphRequest(@Nullable AccessToken accessToken, java.lang.String graphPath, android.os.Bundle parameters, HttpMethod httpMethod, GraphRequest.Callback callback, java.lang.String version)
accessToken
- the access token to use, or nullgraphPath
- the graph path to retrieve, create, or deleteparameters
- additional parameters to pass along with the Graph API request; parameters
must be Strings, Numbers, Bitmaps, Dates, or Byte arrays.httpMethod
- the HttpMethod
to use for the request, or null for default
(HttpMethod.GET)callback
- a callback that will be called when the request is completed to handle
success or error conditionsversion
- the version of the Graph APIpublic static GraphRequest newDeleteObjectRequest(@Nullable AccessToken accessToken, java.lang.String id, GraphRequest.Callback callback)
accessToken
- the access token to use, or nullid
- the id of the object to deletecallback
- a callback that will be called when the request is completed to handle
success or error conditionspublic static GraphRequest newMeRequest(@Nullable AccessToken accessToken, GraphRequest.GraphJSONObjectCallback callback)
accessToken
- the access token to use, or nullcallback
- a callback that will be called when the request is completed to handle
success or error conditionspublic static GraphRequest newPostRequest(@Nullable AccessToken accessToken, java.lang.String graphPath, org.json.JSONObject graphObject, GraphRequest.Callback callback)
accessToken
- the access token to use, or nullgraphPath
- the graph path to retrieve, create, or deletegraphObject
- the graph object to create or updatecallback
- a callback that will be called when the request is completed to handle
success or error conditionspublic static GraphRequest newMyFriendsRequest(@Nullable AccessToken accessToken, GraphRequest.GraphJSONArrayCallback callback)
accessToken
- the access token to use, or nullcallback
- a callback that will be called when the request is completed to handle
success or error conditionspublic static GraphRequest newGraphPathRequest(@Nullable AccessToken accessToken, java.lang.String graphPath, GraphRequest.Callback callback)
accessToken
- the access token to use, or nullgraphPath
- the graph path to retrievecallback
- a callback that will be called when the request is completed to handle
success or error conditionspublic static GraphRequest newPlacesSearchRequest(@Nullable AccessToken accessToken, android.location.Location location, int radiusInMeters, int resultsLimit, java.lang.String searchText, GraphRequest.GraphJSONArrayCallback callback)
accessToken
- the access token to use, or nulllocation
- the location around which to search; only the latitude and longitude
components of the location are meaningfulradiusInMeters
- the radius around the location to search, specified in meters; this is
ignored if no location is specifiedresultsLimit
- the maximum number of results to returnsearchText
- optional text to search for as part of the name or type of an objectcallback
- a callback that will be called when the request is completed to handle
success or error conditionsFacebookException
- If neither location nor searchText is specifiedpublic static GraphRequest newUploadPhotoRequest(@Nullable AccessToken accessToken, java.lang.String graphPath, android.graphics.Bitmap image, java.lang.String caption, android.os.Bundle params, GraphRequest.Callback callback)
accessToken
- the access token to use, or nullgraphPath
- the graph path to use, defaults to me/photosimage
- the bitmap image to uploadcaption
- the user generated caption for the photo, can be nullparams
- the parameters, can be nullcallback
- a callback that will be called when the request is completed to handle
success or error conditions, can be nullpublic static GraphRequest newUploadPhotoRequest(@Nullable AccessToken accessToken, java.lang.String graphPath, java.io.File file, java.lang.String caption, android.os.Bundle params, GraphRequest.Callback callback) throws java.io.FileNotFoundException
accessToken
- the access token to use, or nullgraphPath
- the graph path to use, defaults to me/photosfile
- the file containing the photo to uploadcaption
- the user generated caption for the photo, can be nullparams
- the parameters, can be nullcallback
- a callback that will be called when the request is completed to handle
success or error conditions, can be nulljava.io.FileNotFoundException
- if the file doesn't existpublic static GraphRequest newUploadPhotoRequest(@Nullable AccessToken accessToken, java.lang.String graphPath, android.net.Uri photoUri, java.lang.String caption, android.os.Bundle params, GraphRequest.Callback callback) throws java.io.FileNotFoundException
accessToken
- the access token to use, or nullgraphPath
- the graph path to use, defaults to me/photosphotoUri
- the file:// or content:// Uri to the photo on devicecaption
- the user generated caption for the photo, can be nullparams
- the parameters, can be nullcallback
- a callback that will be called when the request is completed to handle
success or error conditions, can be nulljava.io.FileNotFoundException
- if the Uri does not existpublic static GraphRequest newCustomAudienceThirdPartyIdRequest(@Nullable AccessToken accessToken, android.content.Context context, java.lang.String applicationId, GraphRequest.Callback callback)
FacebookSdk.setLimitEventAndDataUsage(android.content.Context, boolean)
;}
with `true` for this user. You must call this method from a background thread for it to
work properly.accessToken
- the access token to issue the Request on, or null If there is no
logged-in Facebook user, null is the expected choice.context
- the Application context from which the app ID will be pulled, and from
which the 'attribution ID' for the Facebook user is determined. If
there has been no app ID set, an exception will be thrown.applicationId
- explicitly specified Facebook App ID. If null, the application ID from
the access token will be used, if any; if not, the application ID from
metadata will be used.callback
- a callback that will be called when the request is completed to handle
success or error conditions. The GraphObject in the Response will
contain a "custom_audience_third_party_id" property that represents the
user as described above.public static GraphRequest newCustomAudienceThirdPartyIdRequest(@Nullable AccessToken accessToken, android.content.Context context, GraphRequest.Callback callback)
FacebookSdk.setLimitEventAndDataUsage(android.content.Context, boolean)
with
`true` for this user. You must call this method from a background thread for it to work
properly.accessToken
- the access token to issue the Request on, or null If there is no logged-in
Facebook user, null is the expected choice.context
- the Application context from which the app ID will be pulled, and from
which the 'attribution ID' for the Facebook user is determined. If there
has been no app ID set, an exception will be thrown.callback
- a callback that will be called when the request is completed to handle
success or error conditions. The GraphObject in the Response will contain
a "custom_audience_third_party_id" property that represents the user as
described above.public final org.json.JSONObject getGraphObject()
public final void setGraphObject(org.json.JSONObject graphObject)
graphObject
- the GraphObject to upload along with this requestpublic final java.lang.String getGraphPath()
public final void setGraphPath(java.lang.String graphPath)
graphPath
- the graph path for this requestpublic final HttpMethod getHttpMethod()
HttpMethod
to use for this request.public final void setHttpMethod(HttpMethod httpMethod)
HttpMethod
to use for this request.httpMethod
- the HttpMethod, or null for the default (HttpMethod.GET).public final java.lang.String getVersion()
public final void setVersion(java.lang.String version)
version
- The version to use. Should look like "v2.0"public final void setSkipClientToken(boolean skipClientToken)
public final android.os.Bundle getParameters()
public final void setParameters(android.os.Bundle parameters)
parameters
- the parameters@Nullable public final AccessToken getAccessToken()
public final void setAccessToken(AccessToken accessToken)
accessToken
- the access token to use for this requestpublic final java.lang.String getBatchEntryName()
public final void setBatchEntryName(java.lang.String batchEntryName)
batchEntryName
- the name of this requests entry in a batched request, which must be
unique within a particular batch of requestspublic final java.lang.String getBatchEntryDependsOn()
public final void setBatchEntryDependsOn(java.lang.String batchEntryDependsOn)
batchEntryDependsOn
- the name of the request entry that this entry depends on in a
batched requestpublic final boolean getBatchEntryOmitResultOnSuccess()
public final void setBatchEntryOmitResultOnSuccess(boolean batchEntryOmitResultOnSuccess)
batchEntryOmitResultOnSuccess
- the name of the request entry that this entry depends on
in a batched requestpublic static final java.lang.String getDefaultBatchApplicationId()
public static final void setDefaultBatchApplicationId(java.lang.String applicationId)
applicationId
- the Facebook application ID to use for batched requests if none can
be determinedpublic final GraphRequest.Callback getCallback()
public final void setCallback(GraphRequest.Callback callback)
callback
- the callbackpublic final void setTag(java.lang.Object tag)
tag
- an object to serve as a tag, or nullpublic final java.lang.Object getTag()
public final GraphResponse executeAndWait()
FacebookException
- If there was an error in the protocol used to communicate
with the servicejava.lang.IllegalArgumentException
public final GraphRequestAsyncTask executeAsync()
setCallback
method).
This should only be called from the UI thread.java.lang.IllegalArgumentException
public static java.net.HttpURLConnection toHttpConnection(GraphRequest... requests)
requests
- one or more Requests to serializeFacebookException
- If any of the requests in the batch are badly constructed or
if there are problems contacting the servicejava.lang.IllegalArgumentException
- if the passed in array is zero-lengthjava.lang.NullPointerException
- if the passed in array or any of its contents are nullpublic static java.net.HttpURLConnection toHttpConnection(java.util.Collection<GraphRequest> requests)
requests
- one or more Requests to serializeFacebookException
- If any of the requests in the batch are badly constructed or
if there are problems contacting the servicejava.lang.IllegalArgumentException
- if the passed in collection is emptyjava.lang.NullPointerException
- if the passed in collection or any of its contents are nullpublic static java.net.HttpURLConnection toHttpConnection(GraphRequestBatch requests)
requests
- a RequestBatch to serializeFacebookException
- If any of the requests in the batch are badly constructed or
if there are problems contacting the servicejava.lang.IllegalArgumentException
public static GraphResponse executeAndWait(GraphRequest request)
request
- the Request to executeFacebookException
- If there was an error in the protocol used to communicate with the
servicepublic static java.util.List<GraphResponse> executeBatchAndWait(GraphRequest... requests)
requests
- the Requests to executejava.lang.NullPointerException
- In case of a null requestFacebookException
- If there was an error in the protocol used to communicate with
the servicepublic static java.util.List<GraphResponse> executeBatchAndWait(java.util.Collection<GraphRequest> requests)
requests
- the Requests to executeFacebookException
- If there was an error in the protocol used to communicate with the
servicepublic static java.util.List<GraphResponse> executeBatchAndWait(GraphRequestBatch requests)
requests
- the batch of Requests to executeFacebookException
- If there was an error in the protocol used to communicate
with the servicejava.lang.IllegalArgumentException
- if the passed in RequestBatch is emptyjava.lang.NullPointerException
- if the passed in RequestBatch or any of its contents are
nullpublic static GraphRequestAsyncTask executeBatchAsync(GraphRequest... requests)
setCallback
method).
This should only be called from the UI thread.requests
- the Requests to executejava.lang.NullPointerException
- If a null request is passed inpublic static GraphRequestAsyncTask executeBatchAsync(java.util.Collection<GraphRequest> requests)
setCallback
method).
This should only be called from the UI thread.requests
- the Requests to executejava.lang.IllegalArgumentException
- if the passed in collection is emptyjava.lang.NullPointerException
- if the passed in collection or any of its contents are nullpublic static GraphRequestAsyncTask executeBatchAsync(GraphRequestBatch requests)
setCallback
method).
This should only be called from the UI thread.requests
- the RequestBatch to executejava.lang.IllegalArgumentException
- if the passed in RequestBatch is emptyjava.lang.NullPointerException
- if the passed in RequestBatch or any of its contents are
nullpublic static java.util.List<GraphResponse> executeConnectionAndWait(java.net.HttpURLConnection connection, java.util.Collection<GraphRequest> requests)
connection
- the HttpURLConnection that the requests were serialized intorequests
- the requests represented by the HttpURLConnectionFacebookException
- If there was an error in the protocol used to communicate with the
servicepublic static java.util.List<GraphResponse> executeConnectionAndWait(java.net.HttpURLConnection connection, GraphRequestBatch requests)
connection
- the HttpURLConnection that the requests were serialized intorequests
- the RequestBatch represented by the HttpURLConnectionFacebookException
- If there was an error in the protocol used to communicate with the
servicepublic static GraphRequestAsyncTask executeConnectionAsync(java.net.HttpURLConnection connection, GraphRequestBatch requests)
setCallback
method).
This should only be called from the UI thread.connection
- the HttpURLConnection that the requests were serialized intorequests
- the requests represented by the HttpURLConnectionpublic static GraphRequestAsyncTask executeConnectionAsync(android.os.Handler callbackHandler, java.net.HttpURLConnection connection, GraphRequestBatch requests)
setCallback
method)
This should only be called from the UI thread.callbackHandler
- a Handler that will be used to post calls to the callback for each
request; if null, a Handler will be instantiated on the calling
threadconnection
- the HttpURLConnection that the requests were serialized intorequests
- the requests represented by the HttpURLConnectionpublic java.lang.String toString()
toString
in class java.lang.Object