Android facebook图形批处理api

Android facebook图形批处理api,android,Android,我正在尝试使用graph批处理api,是否有任何参考代码?我们如何设置 参数?有人使用过android应用程序的批处理api吗 我正在使用 我还使用了单独的图形API,例如 fbApiObj.request("me/notifications"); fbApiObj.request("me/home");fbApiObj.request("me/friends"); 我想批量生产。关于如何转换为api调用,上面链接中提供的解释不是很清楚。来自facebook graph api的批处理请求可以

我正在尝试使用graph批处理api,是否有任何参考代码?我们如何设置 参数?有人使用过android应用程序的批处理api吗

我正在使用 我还使用了单独的图形API,例如

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

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

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

这是一项非常新的功能,facebook android sdk最近在github中没有更新,因此您需要直接处理这些请求


参考:

您需要做的是为您的请求构建一个JSONArray,然后在使用HTTPS POST将该JSONArray发送到服务器之前将其转换为字符串。对于每个请求,根据Facebook API(之前发布的链接)创建一个JSONObject,然后将所有这些JSONObject添加到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,我们需要将其转换为实际post请求的字符串

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

希望你能从中有所收获…

希望更新的信息能让事情变得更清楚你必须用你自己的“相对url”替换我创建的JSONObject。我改变了JSONArray以更好地适应您的批处理,而不是我的“search?type=checkin”。