Twitter4j interfaces and class structure

2019-09-16 23:22发布

The twitter4j packages are not clear to me. For instance how would I retrieve 500 tweets containing a certain hashtag? How would I declare a user and get all their tweets in the past week? For example, I would like the following:

User brit = new User("Britney Spears");
List<Tweet> britTweets = brit.getAllTweets();   //assuming this method exists

My problem is I cannot instantiate a User since it is declared abstract! What I see so far is a bunch of interfaces with a set of great functions that I do not know how to use.

  1. Could someone show me the structure of this library so I can figure out how to use it?
  2. What objects can I instantiate to use methods from the trending, User, and timeline interfaces?

1条回答
仙女界的扛把子
2楼-- · 2019-09-16 23:46

You can start by looking at the Code Examples in the Twitter4J documentation. Note that for some examples you need to have OAuth credentials configured in twitter4j.properties.

Basically, everything starts with a Twitter object.

Twitter twitter = new TwitterFactory().getInstance();

By looking at the javadoc, you can see that the TwitterImpl returned by the factory method implements lots of different interfaces, so there are many, many methods available through the twitter reference. It looks like TimelineMethods are what you want, specifically the getUserTimeline method that takes a userId as a parameter, or the one that takes a user's screenName.

Also check out what's available in the TrendsMethods and SearchMethods interfaces (both also implemented by the TwitterImpl class).

查看更多
登录 后发表回答