你需要做的是建立一個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僅僅是一個的AsyncTask(只需使用任何線程你想真的...)。重要的部分是HTTPS請求,我使用facebook SDK中已有的那些,就像這樣。
的「PARAMS [0]的ToString()」是JSONArray我發送到的AsyncTask,我們需要轉換爲用於實際發佈請求的字符串。
/* 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();
}
希望你能得到的東西這...
希望更新後的信息讓事情變得清晰 – Kavitha 2011-06-16 22:09:12