简体   繁体   English

使用Twitter4j获取特定推文的收藏计数

[英]Using Twitter4j to get favorited count for a particular tweet

I am new to the twitter4j api, is there any way of getting no of favorite count for a particular tweet using twitter4j. 我是twitter4j api的新手,是否有任何方法可以使用twitter4j来获取特定推文的最爱计数。 I am using api version 3.0.3 for Twitter4j. 我使用的是Twitter4j的api版本3.0.3。

In documentation the method getFavoriteCount() is present but the same method gives compilation error in code. 在文档中,存在方法getFavoriteCount() ,但是相同的方法在代码中给出了编译错误。 Should I use different version of jar file? 我应该使用其他版本的jar文件吗?

 public static void main(String[] args) {

     ConfigurationBuilder cb= new ConfigurationBuilder();
        cb.setDebugEnabled(true);
        cb.setOAuthConsumerKey("**************************");
        cb.setOAuthConsumerSecret("**************************");
        cb.setOAuthAccessToken("*******************************");
        cb.setOAuthAccessTokenSecret("*****************************");
        Twitter  twitter = new TwitterFactory(cb.build()).getInstance();
         User user = null;
        try {
            user = twitter.verifyCredentials();
        } catch (TwitterException e1) {
            e1.printStackTrace();
        }

        String[] test = new String[]{"teststr"};
        ResponseList<User> users;
        try {
            users = twitter.lookupUsers(test);

        for (User user1 : users) {
              if (user1.getStatus() != null)
              {
                  Paging paging = new Paging(1, 40);
                  ResponseList<Status> statusess =   twitter.getUserTimeline(user1.getName(),paging);

              for (Status status3 : statusess)
               {
                long retweetCount = status3.getRetweetCount();
                long favoriteCount = status3.getFavoriteCount();/** this line gives compilation error saying the method getFavoriteCount() is undefined for the type Status**/

               }
              }
        }
        } catch (TwitterException e) {
            e.printStackTrace();
        }

 }

Unfortunately, it seems like the JavaDocs on the Twitter4J site linked next to the 3.0.3 release are not correct. 不幸的是,似乎链接到3.0.3版本旁边的Twitter4J网站上的JavaDocs不正确。 The actual docs are here - you can see that getFavoriteCount() isn't present in these. 实际的文档在这里 -您可以看到其中不存在getFavoriteCount()

It looks like Status#getFavoriteCount() was only introduced in 3.0.4 . 看起来Status#getFavoriteCount()在3.0.4中引入 So yes, you need to upgrade from 3.0.3 in order to use this method. 因此,是的,您需要从3.0.3升级才能使用此方法。

Although be aware that version 3.0.4 looks like it is under active development presently. 尽管要知道3.0.4版目前似乎正在积极开发中。

Although you have already found and marked an answer, let me provide an alternate to switching the Twitter4J version. 尽管您已经找到并标记了答案,但让我提供另一种切换Twitter4J版本的方法。

When using a method similar to what you are using, I had searched a lot and finally found the solution in an entirely unrelated post. 当使用与您使用的方法类似的方法时,我进行了很多搜索,最后在一个完全无关的帖子中找到了解决方案。 Here is how you can get the Favorite count of tweets: 您可以通过以下方式获取最喜欢的推文计数:

NOTE: This is what I use in an Android app of mine. 注意: 这就是我在我的Android应用程序中使用的。

First, in your ConfigurationBuilder cb instance, add this one line: 首先,在您的ConfigurationBuilder cb实例中,添加以下这一行:

cb.setJSONStoreEnabled(true);

This will return all results in a JSON format. 这将以JSON格式返回所有结果。 Strangely, getting a JSON result it provides you that value. 奇怪的是,获取JSON结果会为您提供该值。

Now, to fetch the Tweets : 现在,获取推文

try {
    Paging paging = new Paging(initPagingOffset, 200);
    statuses = twitter.getHomeTimeline(paging);

    String strTweets = DataObjectFactory.getRawJSON(statuses);
    JSONArray JATweets = new JSONArray(strTweets);

    for (int i = 0; i < JATweets.length(); i++) {
        JSONObject JOTweets = JATweets.getJSONObject(i);

        ..... // PARSE ANY OTHER DATA YOU MIGHT NEED FOR DISPLAYING THE TWEETS

        String FAV_COUNT = JOTweet.getString("favorite_count");

    }
} catch (TwitterException e) {
    e.printStackTrace();
} catch (JSONException e) {
    e.printStackTrace();
}

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

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