我正在尝试制作一个草图,从特定用户的Twitter时间线获取最后100条推文。我正在使用Twitter4j并且草图工作正常但我已经知道Twitter4J默认情况下将时间轴结果限制为20.
我已经看过这个页面,了解在使用查询时获得超过100个,但在尝试获取特定用户的时间轴时似乎有所不同。 How to retrieve more than 100 results using Twitter4j
感谢您的期待!
这是我的草图:
import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.json.*;
import twitter4j.internal.util.*;
import twitter4j.management.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;
import twitter4j.internal.json.*;
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("XXXXXX");
cb.setOAuthConsumerSecret("XXXXXX");
cb.setOAuthAccessToken("XXXXXX");
cb.setOAuthAccessTokenSecret("XXXXXX");
java.util.List statuses = null;
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
String userName ="XXXXXX";
int numTweets = 100;
String[] twArray = new String[numTweets];
try {
statuses = twitter.getUserTimeline(userName);
}
catch(TwitterException e) {
}
for (int i=0; i<statuses.size(); i++) {
Status status = (Status)statuses.get(i);
//println(status.getUser().getName() + ": " + status.getText());
twArray[i] = status.getUser().getName() + ": " + status.getText();
}
println(twArray);
答案 0 :(得分:4)
我知道这个问题已经过时了,但是接受的答案并不是最好的解决方案,下面是一个更优雅的代码来解决这个问题。
...
Paging p = new Paging();
p.setCount(100);
return twitter.getUserTimeline("screenName",p);
答案 1 :(得分:3)
这样的事情怎么样?
Paging pg = new Paging();
String userName = "uzr";
void setup() {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("xxxx");
cb.setOAuthConsumerSecret("xxxx");
cb.setOAuthAccessToken("xxxx");
cb.setOAuthAccessTokenSecret("xxxx");
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
int numberOfTweets = 100;
long lastID = Long.MAX_VALUE;
ArrayList<Status> tweets = new ArrayList<Status>();
while (tweets.size () < numberOfTweets) {
try {
tweets.addAll(twitter.getUserTimeline(userName,pg));
println("Gathered " + tweets.size() + " tweets");
for (Status t: tweets)
if(t.getId() < lastID) lastID = t.getId();
}
catch (TwitterException te) {
println("Couldn't connect: " + te);
};
pg.setMaxId(lastID-1);
}
}