繁体   English   中英

Android facebook图批api

[英]Android facebook graph batch api

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

我正在使用此链接 ,我也使用了单独的图形apis,如

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

我想批量他们。 上面链接中提供的解释不清楚如何转换为api调用。

您需要做的是为您的请求构建一个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,我们需要将其转换为实际发布请求的String。

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

希望你能得到一些东西......

来自facebook图API的批量请求可通过HTTP请求获得。 请求是否来自Android手机并不重要。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM