9

我正在尝试使用restfb获取所有帖子,我的代码如下

public Connection<Post> publicSearchMessages(Date fromDate, Date toDate) {
    Connection<Post> messages = publicFbClient.fetchConnection("search",
            Post.class,
            Parameter.with("q", "Watermelon"),
            Parameter.with("since", fromDate),
            Parameter.with("until", toDate),
            Parameter.with("type", "post"));

    return messages;
}

这仅提供最新的 25 条帖子消息。

Parameter.with("limit",100 )

如果我设置限制参数,它会提供 100 条消息,但我不想限制获取帖子消息。所以,

无论如何我可以在不设置限制参数的情况下获得与搜索条件匹配的完整帖子列表吗?

4

4 回答 4

5

也许您可以尝试使用循环。FB每次不能超过1000,所以可以使用循环来获取整个feed。像这样使用偏移量:

Parameter.with("limit", 1000));
Parameter.with("offset", offset));

偏移量将是一个变量,其值为 1000,2000,3000...

于 2012-03-30T08:44:05.667 回答
2

没有办法从 FB 获取无限的结果。默认限制设置为 25。如您所知,您可以使用limit参数进行更改。我还没有找到限制搜索网络的上限。也许,您可以将其设置为非常高的数量。

于 2011-11-04T07:55:19.820 回答
0

正如我最近测试的那样,您不必指定任何内容。Connection 类以这种方式实现 Iterable:

  • 获取 25 个结果
  • hasNext 检查是否有下一项要处理
  • 如果没有,它将获取 25 个结果的下一页

所以基本上你需要做的就是:

Connection<Post> messages = publicFbClient.fetchConnection("search",
        Post.class,
        Parameter.with("q", "Watermelon"),
        Parameter.with("since", fromDate),
        Parameter.with("until", toDate),
        Parameter.with("type", "post"));

for (List<Post> feedConnectionPage : messages) {
        for (Post post : myFeedConnectionPage) {
                 // do stuff with post
        }
}

如果您想要某种返回结果的方法,我会非常小心,因为您可能会返回数千个结果并且爬过它们可能需要一些时间(从几秒到几分钟甚至几小时)并且结果对象数组将真的很大。更好的想法是使用一些异步调用并定期检查方法的结果。

Though it seems that parameter "since" is ignored. Posts are fetched from newest to oldest and I think that it somehow leave out this parameter when doing paging.

Hope I made it more clear for you :)

于 2014-09-10T08:30:30.560 回答
0

We have an Iterator object in Post. So we can do it like this:

Connection<Post> messages = publicFbClient.fetchConnection(...) ;
someMethodUsingPage(messages);
    while (messages.hasNext()) {
        messages = facebookClient.fetchConnectionPage(messages.getNextPageUrl(), Post.class);
        someMethodUsingPage(messages);
    }

Then in each messages we'll have next 25 messages.

于 2015-03-18T16:10:26.270 回答