首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Spring社交推特上获得20多个好友?

如何在Spring社交推特上获得20多个好友?
EN

Stack Overflow用户
提问于 2014-10-10 20:28:50
回答 2查看 388关注 0票数 2

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

代码语言:javascript
运行
复制
@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个好友)

EN

回答 2

Stack Overflow用户

发布于 2016-05-10 04:33:24

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

代码语言:javascript
运行
复制
    // ...
    CursoredList<TwitterProfile> friends = twitter.friendOperations().getFriends();
    ArrayList<TwitterProfile> allFriends = friends;
    while (friends.hasNext()) {
        friends = twitter.friendOperations().getFriendsInCursor(friends.getNextCursor());
        allFriends.addAll(friends);
    }
    // process allFriends...
票数 3
EN

Stack Overflow用户

发布于 2014-10-10 20:38:37

肯定还有另一个错误,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(字符串名)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26299466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档