简体   繁体   English

twitter4j setpage()不起作用

[英]twitter4j setpage() not working

I'm using twitter4j version 2.2.5. 我正在使用twitter4j 2.2.5版本。 setPage() doesn't seem to work if I use it with setSince() and setUntil() . setPage()似乎不工作,如果我用它setSince()setUntil() See the following code: 请参见以下代码:

Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
AccessToken accessToken = new AccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
twitter.setOAuthAccessToken(accessToken);
int page = 1;
while(true) {
    Query query = new Query("from:someUser");
    query.setSince("2012-01-01");
    query.setUntil("2012-07-05");
    query.setRpp(100);
    query.setPage(page++);
    QueryResult qr = twitter.search(query);
    List<twitter4j.Tweet> qrTweets = qr.getTweets();
    if(qr.getTweets().size() == 0) break;
    for(twitter4j.Tweet t : qrTweets) {
        System.out.println(t.getId() + " " + t.getText());
    }
}

The code inside the loop is only executed once if I use setSince() and setUntil() methods. 如果我使用setSince()setUntil()方法,则循环中的代码仅执行一次。 But without them, setPage() seems to work and I get more tweet results. 但是没有它们, setPage()似乎可以工作,并且我得到了更多的推文结果。

Any ideas why this is happening? 任何想法为什么会这样?

Your code appears to be working for me. 您的代码似乎对我有用。 It only returns tweets from the past nine days, but that's expected (approximately) according to your comment. 它只会返回过去9天的推文,但是根据您的评论,这是预期的(大约)。

You mentioned in the question that the loop only ran once with rpp=100 , and you said in a comment that all 80 of the user's tweets were returned when rpp=40 . 您在问题中提到循环仅在rpp=100运行一次,并在注释中说,当rpp=40时,将返回用户的所有80条推文。 I think that would indicate that the code is working as expected. 我认为这表明代码正在按预期工作。 If the user only has 80 tweets, the loop only should run once when rpp=100 , and it should execute twice and display all their tweets when rpp=40 . 如果用户只有80条推文,则循环仅在rpp=100时运行一次,并且应该执行两次并在rpp=40时显示其所有推文。 Try displaying the page number as soon as you enter the while loop so you can see how many times the loop runs. 进入while循环后,尝试立即显示页码,以便查看循环运行了多少次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM