如何获取超过25条post

我正在尝试使用restfb获取所有post消息,我的代码如下

public Connection publicSearchMessages(Date fromDate, Date toDate) { Connection 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条post。

Parameter.with(“limit”,100)

如果我设置了limit参数,它会给出100条消息,但我不想限制提取post消息。 所以,

无论如何,我可以获得与搜索条件匹配的完整邮件列表,而无需设置限制参数?

无法从FB获取无限的结果。 默认限制设置为25.如您所知,您可以使用limit参数更改此值。 我没有找到限制搜索网页的上边框。 也许,你可以把它设置得很高。

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

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

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

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

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

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

 Connection messages = publicFbClient.fetchConnection("search", Post.class, Parameter.with("q", "Watermelon"), Parameter.with("since", fromDate), Parameter.with("until", toDate), Parameter.with("type", "post")); for (List feedConnectionPage : messages) { for (Post post : myFeedConnectionPage) { // do stuff with post } } 

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

虽然似乎忽略了参数“since”。 post从最新到最旧提取,我认为在进行分页时它会以某种方式省略此参数。

希望我为你弄清楚:)

我们在Post中有一个Iterator对象。 所以我们可以这样做:

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

然后在每条消息中,我们将接下来有25条消息。