Twitter API

kind of junky

Page 1 of 1

13 Replies - 2506 Views - Last Post: 05 March 2010 - 08:17 AM Rate Topic: -----

#1 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Twitter API

Posted 03 March 2010 - 02:13 AM

Teacher loves to give us projects that push our envelopes and this is definitely one. I have never worked/seen the Twitter API until today and neither have I used Twitter.com for social networking (always used FB) so I am not even 100% on the terminology, such as, difference between a friend/follower/someone you follow. So I tried to put together what I could tonight and it is no where close to compiling but it is 4:10am and I got calc II in the morning so I got to go for the night. If someone could at least go over my code and see where I went wrong and give me some tips that would be amazing. Thanks in advance.

Below is the part of the assignment that I coded for tonight:
"Write an application that will ask a person for their Twitter login and password, then displays a list of people that person follows by Twitter ID, their status and the date of their status..."

import winterwell.jtwitter.*;
import java.awt.*;
import javax.swing.*;

public class TwitterAppOne {
    SinglyLinkedList myList;
    
    public TwitterAppOne() {
        myList = new SinglyLinkedList();
    }
    
    public static void main(String[] args) {
        JFrame = frame = new JFrame("Twitter");
        JButton button = new JButton("Sign In");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String id = JOptionPane.showInputDialog("Username");
                String pw = JOptionPane.showInputDialog("Password");
            )}
        }
        JPanel panel = new JPanel();
        panel.add(button);
        frame.add(panel);
        frame.setSize(300, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    
    Twitter twitter;
    twitter = new Twitter(id, pw);
    
    friendCount = twitter.getFriendIDs(id).size();
    friends = new User[myFriendsCount];
    
    int x = 1;
    for(int i = 0; i < myFriendsCount; i++) {
        String id = friends[i].getScreenName();
        String status = friends[i].getStatus();
        //How to get date of each status update?
        System.out.println("FOLLOWER NUMBER: " + x);
        System.out.println("ID: " + id);
        System.out.println("STATUS: " + status);
        System.out.println("DATE OF STATUS: ");  //Fix this later.
        System.out.println("-----------------------------------------");
        x++;
    }
    //Will the below do the whole for-loop above all at once?
    twitter.getUserDetail(id);
}


This post has been edited by mattlyons: 03 March 2010 - 02:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Twitter API

#2 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Twitter API

Posted 03 March 2010 - 11:02 AM

I haven't ever really seen an Twitter api question on these forums. Hopefully someone is familiar with it :unsure:
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: Twitter API

Posted 03 March 2010 - 11:29 AM

Can you use a third party library? Twitter4J
Was This Post Helpful? 0
  • +
  • -

#4 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Twitter API

Posted 03 March 2010 - 03:48 PM

I have to use the jar file; jtwitter.jar so I guess not since Twitter4J uses twitter4j-core-2.1.0.jar. Are familiar with the classic TwitterAPI?
Was This Post Helpful? 0
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Twitter API

Posted 03 March 2010 - 06:08 PM

First thing you may want to do is look at some of the scopes you have placed your variables within, specifically your id and pw variables. As you declare and initialise these within an ActionEvent, will they be available to you when you send them to the Twitter method? (I assume ActionEvents have a similar scope rule to normal methods)

The second thing you might want to look at is when you come out of your main method. You start doing more coding, but its not in any method as such, just floating in the middle of no where. I would place this in a method which accepts the params id and pw.

This post has been edited by nick2price: 03 March 2010 - 06:14 PM

Was This Post Helpful? 0
  • +
  • -

#6 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Twitter API

Posted 03 March 2010 - 06:19 PM

Okay so I need to declare and initialize them up by where I did the SinglyLinkedList in the constructor? I can do that no problem.
Was This Post Helpful? 0
  • +
  • -

#7 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Twitter API

Posted 03 March 2010 - 06:25 PM

Why do you have this block sitting outside any methods?
    Twitter twitter;
    twitter = new Twitter(id, pw);
    
    friendCount = twitter.getFriendIDs(id).size();
    friends = new User[myFriendsCount];
    
    int x = 1;
    for(int i = 0; i < myFriendsCount; i++) {
        String id = friends[i].getScreenName();
        String status = friends[i].getStatus();
        //How to get date of each status update?
        System.out.println("FOLLOWER NUMBER: " + x);
        System.out.println("ID: " + id);
        System.out.println("STATUS: " + status);
        System.out.println("DATE OF STATUS: ");  //Fix this later.
        System.out.println("-----------------------------------------");
        x++;
    }
    //Will the below do the whole for-loop above all at once?
    twitter.getUserDetail(id);


Also, you've probably seen this already, but just in case:
Here's the JavaDoc for the library

This post has been edited by erik.price: 03 March 2010 - 06:26 PM

Was This Post Helpful? 0
  • +
  • -

#8 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Twitter API

Posted 03 March 2010 - 08:56 PM

Whoops I moved the closing curly-braclet to under all that cause that is what is executed when main is called.

Among other things, the action listener's closing parentheses/brackets is producing an error. I commented it in the code below and fixed the pw/id variables that needed to be declared in the constructor.

import winterwell.jtwitter.*;
import java.awt.*;
import javax.swing.*;

public class TwitterAppOne {
    SinglyLinkedList myList;
    String id;
    String pw;
    
    public TwitterAppOne() {
        myList = new SinglyLinkedList();
        id = "";
        pw = "";
    }
    
    public static void main(String[] args) {
        JFrame frame = new JFrame("Twitter");
        JButton button = new JButton("Sign In");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                id = JOptionPane.showInputDialog("Username");
                pw = JOptionPane.showInputDialog("Password");
            )}  //Illegal start of expression.
        }
        JPanel panel = new JPanel();
        panel.add(button);
        frame.add(panel);
        frame.setSize(300, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    
        Twitter twitter;
        twitter = new Twitter(id, pw);
        
        friendCount = twitter.getFriendIDs(id).size();
        friends = new User[myFriendsCount];
        
        int x = 1;
        for(int i = 0; i < myFriendsCount; i++) {
            String id = friends[i].getScreenName();
            String status = friends[i].getStatus();
            //How to get date of each status update?
            System.out.println("FOLLOWER NUMBER: " + x);
            System.out.println("ID: " + id);
            System.out.println("STATUS: " + status);
            System.out.println("DATE OF STATUS: ");  //Fix this later.
            System.out.println("-----------------------------------------");
            x++;
        }
        //Will the below do the whole for-loop above all at once?
        twitter.getUserDetail(id);
    }
}


This post has been edited by mattlyons: 03 March 2010 - 10:12 PM

Was This Post Helpful? 0
  • +
  • -

#9 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Twitter API

Posted 03 March 2010 - 11:16 PM

This should get you started for now
import java.awt.*;
import javax.swing.*;

public class TwitterAppOne {

    SinglyLinkedList myList;
    
    public TwitterAppOne() {
        myList = new SinglyLinkedList();
    }
    
    public static void main(String[] args) {
        JFrame frame = new JFrame("Twitter");
        JButton button = new JButton("Sign In");
	    String id;
	    String pw;
	    
        button.addActionListener(new ActionListener() {
   
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				id = JOptionPane.showInputDialog("Username");
                pw = JOptionPane.showInputDialog("Password");
 			} 
  
 		}); 
        JPanel panel = new JPanel();
        panel.add(button);
        frame.add(panel);
        frame.setSize(300, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    
        Twitter twitter;
        twitter = new Twitter(id, pw);
        
        friendCount = twitter.getFriendIDs(id).size();
        friends = new User[myFriendsCount];
        
        int x = 1;
        for(int i = 0; i < myFriendsCount; i++) {
            String newId = friends[i].getScreenName();
            String status = friends[i].getStatus();
            //How to get date of each status update?
            System.out.println("FOLLOWER NUMBER: " + x);
            System.out.println("ID: " + id);
            System.out.println("STATUS: " + status);
            System.out.println("DATE OF STATUS: ");  //Fix this later.
            System.out.println("-----------------------------------------");
            x++;
        }
        //Will the below do the whole for-loop above all at once?
        twitter.getUserDetail(id);
    }
}



You will still get errors, but you should be able to figure them out. For instance, one error is cannot find symbol variable friendCound. What do you think that means? Means you havnt given the variable a data type. You need to find out if its a String, int etc in this case I would imagine an int, and you have to do
int friendCount = twitter.getFriendIDs(id).size();

Was This Post Helpful? 0
  • +
  • -

#10 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Twitter API

Posted 04 March 2010 - 06:51 AM

Thanks for the help. I just stared at this for an hour while I worked and changed stuff and tried different things but I couldn't figure it out and get it to run.

I commented in 5 errors.

import winterwell.jtwitter.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TwitterAppOne {
    SinglyLinkedList myList;
    
    public TwitterAppOne() {
        myList = new SinglyLinkedList();
    }
   
    public static void main(String[] args) {
        JFrame frame = new JFrame("Twitter");
        JButton button = new JButton("Sign In");
        	String id;
        	String pw;
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                id = JOptionPane.showInputDialog("Username"); //Can't find variable. Should I make this Final?
                pw = JOptionPane.showInputDialog("Password"); //Can't find variable. Should I make this Final?
            }});
        JPanel panel = new JPanel();
        panel.add(button);
        frame.add(panel);
        frame.setSize(300, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    
        Twitter twitter;
        twitter = new Twitter(id, pw);
        
        int friendCount = twitter.getFriendIDs(id).size();
        Twitter friends = new User[friendCount];  //'User' has error.
                 
        int x = 1;
        for(int i = 0; i < friendCount; i++) {
            String newId = friends[i].getScreenName();  //Error
            String status = friends[i].getStatus();  //Error
            //How to get date of each status update?
            System.out.println("FOLLOWER NUMBER: " + x);
            System.out.println("ID: " + id);
            System.out.println("STATUS: " + status);
            System.out.println("DATE OF STATUS: ");  //Fix this later.
            System.out.println("-----------------------------------------");
            x++;
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#11 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Twitter API

Posted 04 March 2010 - 12:29 PM

These errors don't even really have to do with the TwitterAPI. I just need to get this compiling
Was This Post Helpful? 0
  • +
  • -

#12 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Twitter API

Posted 04 March 2010 - 10:42 PM

I googled and found something and reworked it to fit my project. In their ActionListener() they just put okButton.addActionListener(this). What does 'this' do by itself? It isn't mentioned anywhere else. How can I get it so when the user clicks the okButton, it logs them into Twitter. Everything else works so far except this part. I tried something but it won't compile because of the okButton's ActionListener.

The error is: "Exception in thread "main" java.lang.Error: Unresolved compilation problem: The constructor Twitter(JTextField, JPasswordField) is undefined"

Another thing though, when I put my actual Twitter username and password into my code, the interface comes up and then whenever I click the "OK" button, nothing happens at all. Help please :(

Second error is commented in as well.

Here is the TwitterAPI if you don't have it: http://www.winterwel...witter/javadoc/

import winterwell.jtwitter.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class TwitterAppOne {
    SinglyLinkedList myList;
    
    public TwitterAppOne() {
        myList = new SinglyLinkedList();
    }

    public static void main(String[] args) {
        JFrame frame;
        JLabel labelName;
        JLabel labelPass;
        final JTextField textName;
        final JPasswordField passField;
        JButton okButton;
        JButton cancelButton;
        final Twitter twitter;
        
        JPanel panelOne = new JPanel();
        labelName = new JLabel("Name");
        textName = new JTextField(15);
        panelOne.add(labelName);
        panelOne.add(textName);

        JPanel panelTwo = new JPanel();
        labelPass = new JLabel("Password");
        passField = new JPasswordField(15);
        panelOne.add(labelPass);
        panelOne.add(passField);

        JPanel panelThree = new JPanel();
        okButton = new JButton("OK");
        cancelButton = new JButton("Cancel");
        
        okButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		twitter = new Twitter(textname, passField);
        	}
        });
        cancelButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		System.exit(0);
        	}
        });
        
        panelOne.add(okButton);
        panelOne.add(cancelButton);

        frame = new JFrame("Twitter");
        frame.add(panelOne);
        frame.setSize(600, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        ArrayList friends = new ArrayList();
        ArrayList status = new ArrayList();
        friends.add(twitter.getFriendIDs(textName));  //Error: "The method getFriendIDs(String) in the type Twitter is not applicable for the arguments (JTextField)"
        for(int i = 0; i < friends.size(); i++) {
        	status.add(friends.get(i)); 
        }       
    }
}


This post has been edited by mattlyons: 04 March 2010 - 11:54 PM

Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Twitter API

Posted 05 March 2010 - 04:17 AM

Quote

The error is: "Exception in thread "main" java.lang.Error: Unresolved compilation problem: The constructor Twitter(JTextField, JPasswordField) is undefined"


This means that the Twitter constructor does not take GUI components as parameters. Perhaps you want to grab the text inside those components to send. Not too sure on what the parameters should be, but here is my educated guess.

// You pass the GUI components here, not a String
// To get the text, use the getText and the getPassword
// methods
twitter = new Twitter(textname, passField);

twitter = new Twitter(textname.getText(), new String(passField.getPassword()));



Not that getPassword() returns a char[], so you construct a string.
Was This Post Helpful? 0
  • +
  • -

#14 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Twitter API

Posted 05 March 2010 - 08:17 AM

Thanks dog! For the time being I changed JPasswordField to just another JTextField so I can get the thing working. It is compiling and asking for input and all, but it is always saying "Successful login", even when I do a wrong password. I can't get it to throw the exception. So therefore it isn't reading the "Twitter twitter = new Twitter(...);" line in or something. Matter of fact, Eclipse is telling me in the line above that the 'twitter' variable is not even being read. Why is that? I am thinking that could be where the problem is if it is fixed.

okButton.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
       		try {
       			Twitter twitter = new Twitter(textName.getText(), passField.getText());
       			System.out.println("Successful login");
       		}
       		catch(TwitterException x) {
       			System.out.println("Failed login");
       		}        			
       	}
});


This post has been edited by mattlyons: 05 March 2010 - 08:18 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1