【问题标题】:Android facebook graph batch apiAndroid facebook 图形批处理 api
【发布时间】:2015-01-14 22:04:10
【问题描述】:

我正在尝试使用图形批处理 api,有任何参考代码吗?我们如何设置 参数 ?有没有人参考android应用程序使用批处理api

我正在使用this link 而且我还使用了单独的图形 api,例如

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

我想批量处理它们。上面链接中提供的关于如何转换为api调用的解释不是很清楚。

【问题讨论】:

  • 希望更新的信息能让事情变得清晰

标签: android


【解决方案1】:

您需要为您的请求构建一个 JSONArray,然后将该 JSONArray 转换为字符串,然后再使用 HTTPS POST 将其发送到服务器。对于每个请求,根据 Facebook API(之前发布的链接)创建一个 JSONObject,然后将所有这些 JSONObjects 添加到 JSONArray 并使用 Facebook SDK 内置的“openUrl”方法(位于 SDK 内的 Util 类中)。

这是我为测试批次而构建的一个小示例。

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);

FacebookBatchWorker 只是一个异步任务(只需使用您真正想要的任何线程......)。重要的部分是 HTTPS 请求,我使用了 facebook SDK 中已经可用的请求,就像这样。

“params[0].toString()”是我发送给 AsyncTask 的 JSONArray,我们需要将其转换为字符串以用于实际的发布请求。

/* 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();
}

希望你能从中有所收获……

【讨论】:

  • 您必须将我创建的 JSONObjects 替换为您自己的“relative_url”。而不是我的“search?type=checkin”,你会有你的“我/朋友”。
  • 我更改了 JSONArray 以更好地适应您的批次。
【解决方案2】:

来自 facebook graph API 的批处理请求可通过 HTTP 请求获得。请求是否来自安卓手机并不重要。

这是一个相当新的功能,facebook android sdk 最近还没有在 github 上更新,所以你需要直接处理这些请求。

参考:http://developers.facebook.com/docs/reference/api/batch/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多