处理twitter4j没有文件添加到草图

时间:2013-12-02 16:10:41

标签: java processing twitter4j

当我使用Twitter4j 3并处理2 enter image description here

时,我收到此错误进行处理

我试过浏览错误,但没有人解决它。当我运行我的程序一段时间(30秒)时,它会停止并发出此错误。我在我的代码中使用流API。

我在MacOSX上运行处理。

EDIT ****

到目前为止,这是我的代码......

void setup() {
  size(600, 600); 
  smooth();
  noStroke();
}

void draw() 
{

  GetTweets();
}

void method1()
{
    System.out.println("method1 can be called and it works!!");

}
void method2()
{
    System.out.println("method2 can be called and it works!!");

}

void method3()
{
    System.out.println("method3 can be called and it works!!");

}

void GetTweets()
{

      ConfigurationBuilder cb = new ConfigurationBuilder();
      cb.setOAuthConsumerKey("xxxxxxxxxxxxxx");
      cb.setOAuthConsumerSecret("xxxxxxxxxxxxxx");
      cb.setOAuthAccessToken("xxxxxxxxxxxxxx");
      cb.setOAuthAccessTokenSecret("xxxxxxxxxxxxxx");

      TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
      StatusListener statusListener = new StatusListener() {



      @Override
      public void onStatus(Status status)
      {
      // Here do whatever you want with the status object that is the       
                           //  tweet you got
           System.out.println(status.getUser().getName() + " : " + status.getText());
           if(status.getText().contains("happy"))
           {
             method1();
             System.out.println("A happy tweet");

           }
           if(status.getText().contains("okay"))
           {
              method2()
              System.out.println("A okay tweet");
           }
           if (status.getText().contains("sad"))
           {

             method3();
             System.out.println("A sad tweet ");

           }

      } //en of the onStatus()
      public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
      // should really remove deleted tweets here ...
      }

      public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
      }

      public void onScrubGeo(long userId, long upToStatusId) {
      // should really remove deleted location information here ...
      }

      public void onStallWarning(StallWarning stallWarning) {
      // should really do something about stalls here ...
      System.out.println(stallWarning);
      }
      @Override
      public void onException(Exception ex)
      {
         ex.printStackTrace();
      }

    }; //end of the listener
    String keywords[] = {"happy","sad","okay"};

        FilterQuery fq = new FilterQuery();
        fq.track(keywords);
        twitterStream.addListener(statusListener);
        twitterStream.filter(fq);


 }

这工作正常,但过了一段时间我得到内存不足错误! 有没有人对如何解决这个问题有任何建议?

提前致谢!

1 个答案:

答案 0 :(得分:0)

程序员,现在有时间检查一下......我不得不对代码进行一些更改才能运行它。我认为问题在于你是从getTweets()调用draw(),因此每秒调用60次(默认值)。在getTweets()内,您可以在每个周期创建一个配置构建器和tweeter流,这是一大批。没必要。 “stream”一旦打开就会“监听”您查询的所有twitters(not really...),并为每个匹配它的推文调用方法onStatus()。所以不需要在平局中调用它。只需打开流......就是这样:)。然后在StatusListener接口中,您实现了您需要完成的推文,每个新推文都将通过该路径。就像你做的那样。只需将界面放在代码中即可。无需在平局中调用它。它的工作方式或多或少类似于mousePressed()方法,每次按下按钮都会调用它。这一个,在每个匹配状态。凉。因此,将实例化移至setup()。在这里我运行了10分钟没有问题......

看(适应代码):

void setup() {
  size(600, 600); 
  smooth();
  noStroke();
  ConfigurationBuilder cb = new ConfigurationBuilder();
  cb.setOAuthConsumerKey("xxxxx");
  cb.setOAuthConsumerSecret("xxxxxx");
  cb.setOAuthAccessToken("xxxxxxx");
  cb.setOAuthAccessTokenSecret("xxxxx"); 


  TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

  String keywords[] = {
    "happy", "sad", "okay"
  };

  FilterQuery fq = new FilterQuery();
  fq.track(keywords);
  twitterStream.addListener(statusListener);
  twitterStream.filter(fq);
}

void draw() {
}

void method1()
{
  System.out.println("method1 can be called and it works!!");
}
void method2()
{
  System.out.println("method2 can be called and it works!!");
}

void method3()
{
  System.out.println("method3 can be called and it works!!");
}

StatusListener statusListener = new StatusListener() {
   @Override
  public void onStatus(Status status) {
    // Here do whatever you want with the status object that is the       
    //  tweet you got
    System.out.println(status.getUser().getName() + " : " + status.getText());
    if (status.getText().contains("happy")) {
      method1();
      System.out.println("A happy tweet");
    }
    if (status.getText().contains("okay")) {
      method2();
      System.out.println("A okay tweet");
    }
    if (status.getText().contains("sad")) {
      method3();
      System.out.println("A sad tweet ");
    }
  } //en of the onStatus()
  public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
    // should really remove deleted tweets here ...
  }

  public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
  }

  public void onScrubGeo(long userId, long upToStatusId) {
    // should really remove deleted location information here ...
  }

  public void onStallWarning(StallWarning stallWarning) {
    // should really do something about stalls here ...
    System.out.println(stallWarning);
  }
   @Override
  public void onException(Exception ex) {
    ex.printStackTrace();
  }
}; //end of the listener