Android facebook图批api

Kav*_*tha 2 android

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

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

fbApiObj.request("me/notifications");
fbApiObj.request("me/home");fbApiObj.request("me/friends");
Run Code Online (Sandbox Code Playgroud)

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

And*_*eas 11

您需要做的是为您的请求构建一个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);
Run Code Online (Sandbox Code Playgroud)

而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();
}
Run Code Online (Sandbox Code Playgroud)

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