6. Doing a Graph request - TomGrill/gdx-facebook GitHub Wiki
After you are logged in (got an access token) you can do Graph API request.
The GDXFacebookGraphRequest
class helps you to build a proper request.
/* Building your request */
GDXFacebookGraphRequest request = new GDXFacebookGraphRequest();
request.setNode("me"); // call against the "me" node.
request.useCurrentAccessToken(); // Set this if you want gdx-facebook to use the currently cached access token.
/* Sending the request */
gdxFacebook.newGraphRequest(request, new GDXFacebookCallback<JsonResult>() {
@Override
public void onSuccess(JsonResult result) {
// Parse Facebook Graph API repsond
JsonValue jsonValue = result.getJsonValue();
// In a real project you should do some validation of the jsonValue. You never know what comes back :)
String fbID = jsonValue.getString("id");
String fbNickname = jsonValue.getString("name");
}
@Override
public void onError(GDXFacebookError error) {
// Handle request errors here
}
@Override
public void onCancel() {
// Handle request cancels
}
@Override
public void onFail(Throwable t) {
// Handle request fails here
}
});
There is a convenient class called GDXFacebookCallbackAdapter<JsonResult>
if you dont require all the interface methods.
GDXFacebookGraphRequest
has a lot of methods to modify your request:
request.setUrl(""); // In case you want to use an older Facebook Graph API Url
request.putField("message", "This is my message for you"); // Set a field for a request. Each node allows or requires certain fields. Read the Facebook SDK docs.
request.putFields(..); // Add multiple fields
request.setMethod(HttpMethods.POST); // If you want to make a POST request. For example when posting on someones Facebook wall.
request.setTimeout(20000); // Sets the time in ms before a request times out. Default is 10000
/* You can also chain calls */
request.setUrl("").setMethod(HttpMethods.POST).setTimeout(20000);
GDXFacebookGraphRequest request = new GDXFacebookGraphRequest().setNode("me/feed").useCurrentAccessToken();
request.setMethod(HttpMethods.POST);
request.putField("message", "Hey use this libGDX extensions");
request.putField("link", "https://github.com/TomGrill/gdx-facebook");
request.putField("caption", "gdx-facebook");
gdxFacebook.newGraphRequest(request, new GDXFacebookCallback<JsonResult>() {
@Override
public void onSuccess(JsonResult result) {
// Success
}
@Override
public void onError(GDXFacebookError error) {
// Error
}
@Override
public void onFail(Throwable t) {
// Fail
}
@Override
public void onCancel() {
// Cancel
}
});