简体   繁体   English

使用RestFB仅获取Facebook的最新帖子

[英]Get only latest posts of Facebook using RestFB

I'm using RestFB to get posts from pages of facebook. 我正在使用RestFB从Facebook页面获取帖子。 The problem is I only want the latests posts of the page, let's say, the posts of the last month. 问题是我只想要页面的最新帖子,比方说上个月的帖子。 With my code I get all the history and it take a lot of time. 通过我的代码,我得到了所有的历史记录,并且花费了很多时间。 How do I do that? 我怎么做? I havn't read about a "timestamp" parameter. 我还没有阅读有关“时间戳”参数的信息。 I have tried with the limit parameter without a good result. 我尝试使用limit参数,但效果不佳。

My code is: 我的代码是:

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;
}

Any idea will be welcomed. 任何想法都将受到欢迎。 Thank you. 谢谢。

My first suggestion: Check the timebased pagination here: https://developers.facebook.com/docs/graph-api/using-graph-api 我的第一个建议:在此处检查基于时间的分页: https : //developers.facebook.com/docs/graph-api/using-graph-api

You simply have to add Parameter.with("since", "<timestamp>") to you fetchConnection call. 您只需要将Parameter.with("since", "<timestamp>")到fetchConnection调用中。

Second suggestion: Leave the loop on a saved timestamp. 第二个建议:将循环保留在已保存的时间戳上。 You simply save the TS of the latest post and on the next polling iteration you stop the loop on that TS. 您只需保存最新帖子的TS,并在下一次轮询迭代中停止该TS上的循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM