您需要为您的请求构建一个 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();
}
希望你能从中有所收获……