Twitter4j处理错误

时间:2013-11-18 15:59:08

标签: twitter processing twitter4j

我正在尝试关注this教程:

//Build an ArrayList to hold all of the words that we get from the
 imported tweets 
ArrayList<String> words = new ArrayList();  
 void setup() {   //Set the size of the stage, and the background to black. 
 size(550,550);  
 background(0); 
 smooth();
      //Credentials   ConfigurationBuilder cb = new ConfigurationBuilder();  
 cb.setOAuthConsumerKey("lPFSpjBppo5u4KI5xEXaQ");  
 cb.setOAuthConsumerSecret("SYt3e4xxSHUL1gPfM9bxQIq6Jf34Hln9T1q9KGCPs");
 cb.setOAuthAccessToken("17049577-Yyo3AEVsqZZopPTr055TFdySop228pKKAZGbJDtnV");
 cb.setOAuthAccessTokenSecret("6ZjJBebElMBiOOeyVeh8GFLsROtXXtKktXALxAT0I");
     //Make the twitter object and prepare the query  
 Twitter twitter = new  
 TwitterFactory(cb.build()).getInstance();   
 Query query = new Query("#OWS");  
  query.setRpp(100);
     //Try making the query request.   try {
     QueryResult result = twitter.search(query);
     ArrayList tweets = (ArrayList) result.getTweets();

     for (int i = 0; i < tweets.size(); i++) {
       Tweet t = (Tweet) tweets.get(i);
       String user = t.getFromUser();
       String msg = t.getText();
       Date d = t.getCreatedAt();
       println("Tweet by " + user + " at " + d + ": " + msg);

       //Break the tweet into words
       String[] input = msg.split(" ");
       for (int j = 0;  j < input.length; j++) {
        //Put each word into the words ArrayList
        words.add(input[j]);
       }
     };   }   catch (TwitterException te) {
     println("Couldn't connect: " + te);   }; }   void draw() {   //Draw a faint black rectangle over what is currently on the stage so
 it fades over time.   fill(0,1);   rect(0,0,width,height);
       //Draw a word from the list of words that we've built   int i = (frameCount % words.size());   String word = words.get(i);
       //Put it somewhere random on the stage, with a random size and colour   fill(255,random(50,150));   textSize(random(10,30));  
 text(word, random(width), random(height)); }

但是当我在处理中运行代码时,我收到以下错误。 cannot find class or type named tweet

我通过拖放到处理IDE添加了twitter4j库。

我正在使用处理2.1和twitter4j3.05

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

这是使用twitter4j 3.0.5的基本示例。

import java.util.*;

List<Status>statuses = null;

TwitterFactory twitterFactory;
Twitter twitter;

void setup() {     
  size(100, 100);    
  background(0); 

  connectTwitter();    
  getTimeline();  
  getSearchTweets();
}  

void draw() {     
  background(0);
}  




// Initial connection
void connectTwitter() {  
  ConfigurationBuilder cb = new ConfigurationBuilder();  
  cb.setOAuthConsumerKey("xxx");
  cb.setOAuthConsumerSecret("xxx");
  cb.setOAuthAccessToken("xxx");
  cb.setOAuthAccessTokenSecret("xxx"); 

  twitterFactory = new TwitterFactory(cb.build());    
  twitter = twitterFactory.getInstance();  

  println("connected");
} 

// Get your tweets
void getTimeline() {     
  try {        
    statuses = twitter.getHomeTimeline();
  }   
  catch(TwitterException e) {         
    println("Get timeline: " + e + " Status code: " + e.getStatusCode());
  }     
  for (Status status:statuses) {               
    println(status.getUser().getName() + ": " + status.getText());
  }
}  
// Search for tweets

void getSearchTweets() {           
  try {        
    Query query = new Query("love");            
    QueryResult result = twitter.search(query);              
    for (Status status : result.getTweets()) {              
      println("@" + status.getUser().getScreenName() + ":" + status.getText());
    }
  }   
  catch (TwitterException e) {            
    println("Search tweets: " + e);
  }
}