2011-06-16 69 views
2

我想使用图形批处理api,有没有任何参考代码?我们如何设置 参数?有没有人使用批量API参考Android应用Android的facebook图形批处理api

我使用this link 和我也已经使用单独的图形API,如

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

我想批量他们。以上链接中提供的解释并不十分清楚如何转换为API调用。

+0

希望更新后的信息让事情变得清晰 – Kavitha 2011-06-16 22:09:12

回答

0

从Facebook的图形API的批次要求都可以通过HTTP请求。请求是否来自Android手机并不重要。

这是一个相当最新功能和Facebook的Android SDK中还没有在github上最近更新的,所以你需要直接处理这些请求。

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

11

你需要做的是建立一个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(); 
} 

希望你能得到的东西这...

+0

您必须用您自己的“relative_url”替换我创建的JSONObjects。而不是我的“搜索?类型=签入”你会有你的“我/朋友”。 – Andreas 2011-07-09 08:54:13

+0

我更改了JSONArray以更好地适合您的批处理。 – Andreas 2011-07-09 09:03:51