0

私はここのドキュメントをフォローしようとしていました: https ://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/tweets/ShowStatus.javaどこか。ただし、ドキュメントとは少し異なることをしようとしています。私は引数を取らず、代わりにハードコードされたユーザー名を使用しています。これが問題のあるコードです。

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 の追加のキャッチを追加する必要があります。

編集

ユーザーの最新のツイートを取得するには、次の方法を使用できます。

まず、リクエストのページングを定義します。この場合、1 ページに 1 つのツイートと、1 ページに 1 つのツイートを要求するだけです (間違っていなければ、最新のツイートになります)。次に、ツイートを参照していて、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 の入力は文字列形式ですが、文字列内の文字はすべて 10 進数でなければなりませんrye761。したがって、例外が発生しています

文字列内の文字はすべて 10 進数でなければなりませんが、最初の文字は負の値を示す ASCII マイナス記号 '-' (\u002D') にすることができます。

于 2012-08-13T14:29:33.510 に答える