【问题标题】:Twitter API Twitter4j getUserIDTwitter API Twitter4j getUserID
【发布时间】:2014-02-12 12:44:10
【问题描述】:

我正在使用 Twitter4j 从我关注的用户那里获取推文。每次获得 1000 个,但我有点纠结于如何在输出中包含用户 ID 和用户名。

这是我用来获取推文的代码:

try {
            ResponseList<Status> a = twitter.getHomeTimeline(new Paging(1,1000));

            for (Status b: a){
                System.out.println(b.getText());
            }
        }

有人知道我必须添加什么才能输出 ID、用户名和推文吗?

谢谢 Z19

【问题讨论】:

    标签: java twitter twitter4j


    【解决方案1】:

    您可以使用以下方法获取 id 和用户名。

    User user = b.getUser() --> 返回与状态关联的用户。 然后使用user.getId()user.getName() 可以得到id 和用户名。

    try {
                ResponseList<Status> a = twitter.getHomeTimeline(new Paging(1,1000));
    
                for (Status b: a){
                    long userId = b.getUser().getId();// user Id
                    String userName = b.getUser().getName(); // user name
                    String tweetText = b.getText(); // tweet
    
                    System.out.println(userId+" "+userName+" "+tweetText);
                }
            }
    

    有关更多信息,您可以参考以下链接:

    1. Twitter 4j Status
    2. Twitter 4j User

    【讨论】:

    • 你在这里帮助了我,工作得很好。谢谢你,我很感激。
    • 虽然第一个 String userID 需要是“long”;非常感谢。
    猜你喜欢
    • 2013-06-09
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 2013-05-04
    • 2017-05-18
    • 2014-03-09
    • 2014-01-07
    相关资源
    最近更新 更多