0

我试图在这里关注文档:https ://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/tweets/ShowStatus.java但似乎出了问题某处。但是,我正在尝试做一些与文档略有不同的事情。我没有使用 args,而是使用硬编码的用户名。这是令人不安的代码。

import twitter4j.Twitter;
import twitter4j.Status;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

public class ChrisTwitter {
    public Status status;
    public ChrisTwitter (){
        Twitter twitter = new TwitterFactory().getInstance();
        try {
            Status status = twitter.showStatus(Long.parseLong("rye761"));
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        }
        catch (TwitterException e) {
            e.printStackTrace();
        }
    }
}

有任何想法吗?哦,这是我在控制台中得到的:(新堆栈跟踪)

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: 
    TwitterFactory.getInstance cannot be resolved to a type
    The method Page(int, int) is undefined for the type ChrisTwitter

    at com.github.ryebread761.lockergnome.ChrisTwitter.<init>(ChrisTwitter.java:16)
    at com.github.ryebread761.lockergnome.Base$CTListener.actionPerformed(Base.java:121)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6375)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6140)
    at java.awt.Container.processEvent(Container.java:2083)
    at java.awt.Component.dispatchEventImpl(Component.java:4737)
    at java.awt.Container.dispatchEventImpl(Container.java:2141)
    at java.awt.Component.dispatchEvent(Component.java:4565)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
    at java.awt.Container.dispatchEventImpl(Container.java:2127)
    at java.awt.Window.dispatchEventImpl(Window.java:2482)
    at java.awt.Component.dispatchEvent(Component.java:4565)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:643)
    at java.awt.EventQueue$1.run(EventQueue.java:641)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:657)
    at java.awt.EventQueue$2.run(EventQueue.java:655)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:654)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
4

2 回答 2

1

您正在尝试将字母数字字符串解析为 long,这会引发 NumberFormatException:

Status status = twitter.showStatus(Long.parseLong("rye761"));

由于您没有在 try-catch 块中捕获 NumberFormatException 异常传播。为了防止这种情况发生,您应该在这样做之前验证您尝试解析的输入,或者为该 NumberFormatException 添加一个额外的捕获。

编辑

要获取用户的最新推文,您可以采用以下方法:

首先定义你的请求的分页。在这种情况下,只需要求一页和每页一条推文(如果我没记错的话,它将是最新的)。然后您直接发出请求,因为您正在咨询推文并且您没有做任何其他您不需要验证 AFAIK 的事情。

Twitter latestTweetChecker = new TwitterFactory.getInstance();
Paging page = Page(1,1);
List<Status> statusList = latestTweetChecker.getUserTimeline("rye761",page);

在那里,您将获得所需的状态。只需通过相应的方法获取您需要的信息。

于 2012-08-13T14:29:43.160 回答
0

Long.parseLong ("rye761")

尽管 parseLong 的输入格式为 String,但 String 中的字符必须全部为十进制数字,而rye761. 所以,你得到了例外

字符串中的字符必须都是十进制数字,除了第一个字符可以是 ASCII 减号“-”(\u002D')来表示负值。

于 2012-08-13T14:29:33.510 回答