[英]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.