【问题标题】:How to get more than 20 friends in Spring Social Twitter?如何在 Spring Social Twitter 中获得 20 多个好友?
【发布时间】:2014-12-05 15:01:24
【问题描述】:

我正在使用 Spring Social Twitter 检索用户的朋友姓名。 这是我的代码。

@Controller
@RequestMapping("/")
   public class HelloController {

    private Twitter twitter;

    private ConnectionRepository connectionRepository;

    @Inject
    public HelloController(Twitter twitter, ConnectionRepository connectionRepository) {
        this.twitter = twitter;
        this.connectionRepository = connectionRepository;
    }

    @RequestMapping(method=RequestMethod.GET)
    public String helloTwitter(Model model) {
        if (connectionRepository.findPrimaryConnection(Twitter.class) == null) {
            return "redirect:/connect/twitter";
        }

        model.addAttribute(twitter.userOperations().getUserProfile());
        CursoredList<TwitterProfile> friends = twitter.friendOperations().getFriends();
        model.addAttribute("friends", friends);
        for ( TwitterProfile frnd : friends) {
            System.out.println(frnd.getName());
        }
        return "hello";
    }

}

但它只检索到 20 个朋友。我怎么能得到所有的朋友? (假设我有 1000 个朋友)

【问题讨论】:

  • 我有一个模糊的记忆,Twitter APIs 页面这种数据,因此您必须通过 X 元素块迭代检索它(在这种情况下,显然是 20 个)。对您帮助不大,但 Twitter API 页面中应该有一些关于它的文档。
  • 我试图为此找到一个文档。还是找不到。
  • 根据to the docs,此方法应使用多次调用 Twitter API 检索多达 5000 个用户。
  • 我试过'friends.size()'。它返回 20。
  • 我也有同样的问题,并为该案例找到了解决方案。检查我的帖子.. link for my post

标签: java spring twitter spring-social spring-social-twitter


【解决方案1】:

您必须遍历所有游标并收集结果如下:

    // ...
    CursoredList<TwitterProfile> friends = twitter.friendOperations().getFriends();
    ArrayList<TwitterProfile> allFriends = friends;
    while (friends.hasNext()) {
        friends = twitter.friendOperations().getFriendsInCursor(friends.getNextCursor());
        allFriends.addAll(friends);
    }
    // process allFriends...

【讨论】:

  • 您应该使用 getFriendIdsInCursor 以不超过速率限制。
【解决方案2】:

肯定还有错误,spring文档特别说明:

getFriends()

“检索经过身份验证的用户关注的最多 5000 个用户的列表。”

http://docs.spring.io/spring-social-twitter/docs/1.0.5.RELEASE/api/org/springframework/social/twitter/api/FriendOperations.html#getFriendIds%28%29

您确定与您进行查询的用户有更多朋友吗?也许你可以尝试使用 getFriendsIds 或 getFriends(string name)。

【讨论】:

    猜你喜欢
    • 2014-01-22
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多