使用MaxId从最后一次获取中获取特定用户的推文

时间:2014-02-17 14:28:38

标签: java twitter4j

我们正在使用twitter4j userTimeLine来获取特定用户的推文。我如何使用maxId以及如何从上次提取推文中获取推文???

我的源代码如下,

public List<Status> userTimeLine(String keyWord, int page, int count) {

        log.info("Showing user timeline.");

        List<Status> statuses = new ArrayList<Status>(0);
        Paging paging = new Paging(page, count);
        try {
            statuses = twitter.getUserTimeline(keyWord, paging);
        } catch (TwitterException e) {
            log.error("Unable to find user timeline", e);
        }

        return statuses;
    }

此代码返回第一次获取的100条推文。在第二次获取中,如果用户发布了新的推文,它将检索102 [100(最后提取的推文)+2(新推文)]。否则,它会为每次提取返回相同的100条推文。

如何解决上次推文推文中的推文?

1 个答案:

答案 0 :(得分:2)

您可以使用Paging指定推文(按状态ID)来获取使用sinceId和maxId方法在其间发布的推文。

 since_id: returns elements which id are bigger than the specified id
 max_id: returns elements which id are smaller than the specified id

例如:

 Paging paging = new Paging(1, 10).sinceId(258347905419730944L).maxId(258348815243960320L);
 List<Status> statuses = twitter.getHomeTimeline(paging);

你可以找到很多东西here:,你也可以像这样使用sthg;

 Query query = new Query("from:somebody").since("2011-01-02");
 Twitter twitter = new TwitterFactory().getInstance();
 QueryResult result = twitter.search(query);