次のコードを参照してください。
//Configuration for twitter4j
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("*********************")
.setOAuthConsumerSecret("******************************************")
.setOAuthAccessToken("**************************************************")
.setOAuthAccessTokenSecret("******************************************");
//Example for fetching public tweets
//Sample which gives all tweets of Kim Kardarshian
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
int pageno = 1;
String user = "@KimKardashian";
List<Status> statuses = new ArrayList<Status>();
while (true) {
try {
int size = statuses.size();
Paging page = new Paging(pageno++, 100);
statuses.addAll(twitter.getUserTimeline(user, page));
if (statuses.size() == size)
break;
}
catch(TwitterException e) {
e.printStackTrace();
}
}
//sample which gives "n" (here 10) tweets of particular user
try{
Twitter twitter = TwitterFactory.getSingleton();
Paging p = new Paging();
p.setCount(10);
statuses.addAll(twitter.getUserTimeline("user",p));
}catch(Exception e)
{ e.printStackTrace(); }
for (Status st:statuses) {
System.out.println(st.getText());
}