本文介绍了如何使用处理和Twitter4j从用户时间轴中获取20多个结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试绘制一个草图,以从特定用户的Twitter时间轴中获取最近的100条推文.我正在使用Twitter4j,并且草图可以正常工作,但是我了解到Twitter4J默认将时间轴结果限制为20.
I'm trying to make a sketch that gets the last 100 tweets from a specific user's twitter timeline. I'm using Twitter4j and the sketch works fine but I've learned that Twitter4J by default limits the timeline results to 20.
我已经看过此页面,以了解使用Queries时获得超过100的信息,但是在尝试获得特定用户的时间轴时,似乎工作方式有所不同.如何使用Twitter4j检索100多个结果
I've seen this page to learn about getting more than 100 when using Queries, but it seems to be working differently when trying to get a specific user's timeline.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);
推荐答案
这样的事情怎么样?
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);
}
}
这篇关于如何使用处理和Twitter4j从用户时间轴中获取20多个结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!