简体   繁体   中英

Twitter4j NumberFormatException

I'm using some of Twitter4j examples in their Github repo, but whenever I'm trying to use any username, for instance to get the last tweet, using:

Twitter twitter = new TwitterFactory().getInstance();
Status status = twitter.showStatus(Long.parseLong("userName"));

it gives me the following error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "userName"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Long.parseLong(Long.java:589)
    at java.lang.Long.parseLong(Long.java:631)

Apparently there is other ways to do the same task, but I wonder if anyone has a solution for this problem. Also I'm using 'org.twitter4j:twitter4j-core:4.0.2' .

As Joey pointed out Long.parseLong("userName") is the problem.
If you use twitter.showStatus(an id) you need the id of the tweet, not the user name. If you want the last tweet by an user you need to do this:

Twitter twitter = new TwitterFactory().getInstance();
User user = twitter.showUser(username); //username could be an String or a Long with the id of the user
Status status = user.getStatus();

And that's it

Long.parseLong("userName") is the problem. You are trying to parse the string "userName" into a Long value which will not work.

I think you're looking for... User.getID() check the following link:

http://twitter4j.org/oldjavadocs/4.0.2/index.html

See: http://twitter4j.org/en/code-examples.html for more examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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