使用 RestFB 仅获取 Facebook 的最新帖子
Get only latest posts of Facebook using RestFB
我正在使用 RestFB 从 facebook 页面获取帖子。问题是我只想要页面的最新帖子,比方说,上个月的帖子。使用我的代码,我获得了所有历史记录,这需要花费很多时间。
我怎么做?我还没有读到 "timestamp" 参数。我尝试使用 limit 参数但没有取得好的结果。
我的代码是:
public static JSONArray GetPostsFromPage(String idpage) {
String texto, idpost;
Date timestamp;
JSONArray array = new JSONArray();
JSONObject objeto;
Connection<Post> postFeed = fbClient.fetchConnection(idpage + "/feed", Post.class, Parameter.with("limit", 50));
for (List<Post> postPage : postFeed) {
for (Post aPost : postPage) {
objeto = new JSONObject();
idpost = aPost.getId();
texto = aPost.getMessage();
timestamp = aPost.getCreatedTime();
objeto.put("id", idpost);
objeto.put("texto", texto);
objeto.put("timestamp", timestamp);
array.put(objeto);
}
}
return array;
}
欢迎任何想法。谢谢。
我的第一个建议:
在此处检查基于时间的分页:
https://developers.facebook.com/docs/graph-api/using-graph-api
您只需将 Parameter.with("since", "<timestamp>")
添加到您的 fetchConnection 调用中。
第二条建议:
将循环留在已保存的时间戳上。您只需保存最新 post 的 TS,并在下一次轮询迭代中停止该 TS 上的循环。
我正在使用 RestFB 从 facebook 页面获取帖子。问题是我只想要页面的最新帖子,比方说,上个月的帖子。使用我的代码,我获得了所有历史记录,这需要花费很多时间。 我怎么做?我还没有读到 "timestamp" 参数。我尝试使用 limit 参数但没有取得好的结果。
我的代码是:
public static JSONArray GetPostsFromPage(String idpage) {
String texto, idpost;
Date timestamp;
JSONArray array = new JSONArray();
JSONObject objeto;
Connection<Post> postFeed = fbClient.fetchConnection(idpage + "/feed", Post.class, Parameter.with("limit", 50));
for (List<Post> postPage : postFeed) {
for (Post aPost : postPage) {
objeto = new JSONObject();
idpost = aPost.getId();
texto = aPost.getMessage();
timestamp = aPost.getCreatedTime();
objeto.put("id", idpost);
objeto.put("texto", texto);
objeto.put("timestamp", timestamp);
array.put(objeto);
}
}
return array;
}
欢迎任何想法。谢谢。
我的第一个建议: 在此处检查基于时间的分页: https://developers.facebook.com/docs/graph-api/using-graph-api
您只需将 Parameter.with("since", "<timestamp>")
添加到您的 fetchConnection 调用中。
第二条建议: 将循环留在已保存的时间戳上。您只需保存最新 post 的 TS,并在下一次轮询迭代中停止该 TS 上的循环。