我正在使用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个好友)
发布于 2016-05-10 04:33:24
您必须遍历所有游标并收集结果,如下所示:
// ...
CursoredList<TwitterProfile> friends = twitter.friendOperations().getFriends();
ArrayList<TwitterProfile> allFriends = friends;
while (friends.hasNext()) {
friends = twitter.friendOperations().getFriendsInCursor(friends.getNextCursor());
allFriends.addAll(friends);
}
// process allFriends...发布于 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(字符串名)。
https://stackoverflow.com/questions/26299466
复制相似问题