我试图按照这里的文档: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)
答案 0 :(得分:1)
您正在尝试将字母数字字符串解析为long,并且抛出NumberFormatException:
Status status = twitter.showStatus(Long.parseLong("rye761"));
由于您没有在try-catch块中捕获NumberFormatException,因此异常会传播。为了防止这种情况,您应该在执行此操作之前验证您尝试解析的输入,或者为该NumberFormatException添加aditional catch。
修改强>
要获取用户的最新推文,您可以采用以下方法:
首先定义请求的分页。在这种情况下,每页只需要一页和一条推文(如果我没弄错的话,它将是最新的一页)。然后你直接发出请求,因为你正在咨询推文而你没有做任何其他事情,你不需要认证AFAIK。
Twitter latestTweetChecker = new TwitterFactory.getInstance();
Paging page = Page(1,1);
List<Status> statusList = latestTweetChecker.getUserTimeline("rye761",page);
在那里,您将拥有所需的状态。只需使用corresponding methods获取所需信息。
答案 1 :(得分:0)
Long.parseLong( “rye761”)
即使parseLong的输入格式为String,字符串中的字符也必须是全部十进制数字,而rye761
不是这种情况。所以,你得到了异常
字符串中的字符必须都是十进制数字,除此之外 第一个字符可以是ASCII减号' - '(\ u002D')来 表示负值。