Netbeans教程:为twitter创建图形客户端

时间:2013-02-14 11:41:36

标签: java swing netbeans-7

我正在练习关于为twitter创建图形客户端的netbeans教程,请参阅以下链接:http://netbeans.org/kb/docs/websvc/twitter-swing.html我正在使用Netbeans 7.1

下面是教程的代码片段

    public class TwitterFrame extends javax.swing.JFrame {

        /**
         * Creates new form TwitterFrame
         */
        public TwitterFrame() {
            initComponents();

             try {
            initUserInfo();
        } catch (IOException ex) {
            Logger.getLogger(TwitterFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        }



     private void initUserInfo() throws MalformedURLException, IOException {

            //Create an instance of the internal service class
        client = new Twitter_OAuth_user_timeline__format_JerseyClient("xml");


        //Log in, get tokens, and append the tokens to the consumer and secret
        //keys
        client.login();
        client.initOAuth();

        //Call getUserTimeline, get a list of statuses, pass the most recent
        //status as a StatusType object, and display the text of that object
        //in the JTextField
        Statuses statuses = client.getUserTimeline(Statuses.class, null, null, null, "1");
            twitter.twitteroauth.twitterresponse.StatusType st = statuses.getStatus().get(0);
        jTextField1.setText(st.getText().trim());

         //Get a UserType object from the StatusType object, get the URL of that
        //user's icon, and display that icon in the JLabel
        UserType user = st.getUser();
        String iconSrc = user.getProfileImageUrl();
        URL iconUrl = new URL(iconSrc);
        ImageIcon icon = new ImageIcon(iconUrl, user.getScreenName());
        jLabel1.setIcon(icon);

    }


 public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TwitterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TwitterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TwitterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TwitterFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TwitterFrame().setVisible(true);
            }
        });
    }

但是当我在netbeans上运行代码时,我得到以下错误。

run:
Exception in thread "AWT-EventQueue-0" com.sun.jersey.api.client.UniformInterfaceException: GET http://twitter.com/oauth/request_token returned a response status of 301 Moved Permanently
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:676)
    at com.sun.jersey.api.client.WebResource.get(WebResource.java:191)
    at twitterclient.TwitterFrame$Twitter_OAuth_user_timeline__format_JerseyClient.getOAuthRequestToken(TwitterFrame.java:268)
    at twitterclient.TwitterFrame$Twitter_OAuth_user_timeline__format_JerseyClient.login(TwitterFrame.java:255)
    at twitterclient.TwitterFrame.initUserInfo(TwitterFrame.java:123)
    at twitterclient.TwitterFrame.<init>(TwitterFrame.java:37)
    at twitterclient.TwitterFrame$2.run(TwitterFrame.java:177)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

任何有关如何运行教程的建议的人。 感谢。

1 个答案:

答案 0 :(得分:2)

我想自从编写Netbeans教程以来,网址已经发生了变化,现在根据Twitter documentationhttps://api.twitter.com/oauth/request_token

该异常表示您致电:http://twitter.com/oauth/request_token

您错过了api.