Kavitha
Kavitha

Reputation: 471

Android facebook graph batch api

I am trying to use graph batch api , is there any reference code ? how do we set the parameters ? Has anyone used batch api with reference to android apps

I am using this link and I've also have used individual graph apis such as

fbApiObj.request("me/notifications");
fbApiObj.request("me/home");fbApiObj.request("me/friends");

I want to batch them. The explanation provided in the link above is not very clear as to how to convert to api calls.

Upvotes: 2

Views: 2308

Answers (2)

Andreas
Andreas

Reputation: 920

What you need to do is build a JSONArray for your request, and then convert that JSONArray to a string before you send it to the server using HTTPS POST. For each request, make a JSONObject according to the Facebook API (link previously posted), then add all these JSONObjects to a JSONArray and use the Facebook SDK's built-in "openUrl" method (located in the Util class inside the SDK).

Here's a small example that I built for testing the batch.

JSONObject me_notifications = new JSONObject();
try {
    me_notifications.put("method", "GET");
    me_notifications.put("relative_url", "me/notifications");
} catch (JSONException e) {
    e.printStackTrace();
    Log.e(TAG, e.getMessage());
}

JSONObject me_home = new JSONObject();
try {
    me_home.put("method", "GET");
    me_home.put("relative_url", "me/home");
} catch (JSONException e) {
    e.printStackTrace();
    Log.e(TAG, e.getMessage());
}

JSONObject me_friends = new JSONObject();
try {
    me_friends.put("method", "GET");
    me_friends.put("relative_url", "me/friends");
} catch (JSONException e) {
    e.printStackTrace();
    Log.e(TAG, e.getMessage());
}

JSONArray batch_array = new JSONArray();
batch_array.put(me_home);
batch_array.put(me_notifications);
batch_array.put(me_friends);

new FacebookBatchWorker(this, mHandler, false).execute(batch_array);

And the FacebookBatchWorker is simply an asynctask (just use any threading you want really...). The important part is the HTTPS request, I used the already available ones inside the facebook SDK, like this.

The "params[0].toString()" is the JSONArray I sent to the AsyncTask, we need that converted to a String for the actual post request.

/* URL */
String url = GRAPH_BASE_URL;

/* Arguments */
Bundle args = new Bundle();
args.putString("access_token", FacebookHelper.getFacebook().getAccessToken());
args.putString("batch", params[0].toString());

String ret = "";

try {
    ret = Util.openUrl(url, "POST", args);
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Hopefully you'll get something out of this...

Upvotes: 11

Aleadam
Aleadam

Reputation: 40401

The batch requests from the facebook graph API are available through HTTP requests. It does not matter whether the request comes from an android phone or not.

It is a quite recent feature and the facebook android sdk has not been updated recently in github, so you will need to handle those requests directly.

Reference: http://developers.facebook.com/docs/reference/api/batch/

Upvotes: 0

Related Questions