问题描述
当我使用 Twitter4j 3 和处理 2 时,我收到此错误进行处理.
Im getting this error for processing, when I use Twitter4j 3 and processing 2.
我试过浏览错误,但没有人解决它.当我运行我的程序一段时间(30 秒)时,它会停止并给出此错误.我在我的代码中使用了流式 API.
Ive tried browsing the error, but no one has resolved it. When ever I run my program for a period of time(30 seconds), it halts and gives this error. Im using the streaming API in my code.
我在 MacOSX 上运行处理.
Im running processing on a MacOSX.
编辑****
这是我目前的代码...
Here is my code so far...
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);
}
这工作正常,但过了一会儿我得到了内存不足的错误!有人对如何解决此问题有任何建议吗?
This works fine but after a while i get the out of memory error!Has anyone got any suggestions on how to fix this?
提前致谢!
推荐答案
程序员,现在正好有时间检查一下...我必须对代码进行一些更改才能运行它.我认为问题在于您正在从 draw()
调用 getTweets()
,因此它每秒被调用 60 次(默认).在 getTweets()
中,您可以创建配置构建器和高音扬声器,每个循环一个,这是一大批.不需要那个.流"一旦打开就会收听"所有推特(不是真的......) 用于您的查询,并将为匹配它的每条推文调用方法 onStatus()
.所以不需要在 draw 中调用它.只需打开流......就是这样:).然后在 StatusListener 接口中,您实现了您需要对推文完成的操作,每个新推文都将通过该路径.正如你所做的那样.只需将接口放在代码中即可.无需在 draw 中调用它.它的工作方式或多或少类似于 mousePressed()
方法,每次按下按钮时都会调用它.这个,在每个匹配的状态上.凉爽的.因此将实例化移至 setup()
.在这里,我让它运行了 10 分钟,没有问题......
Pro-grammer, just had time to check this out now... I had to make some changes in the code to be able to run it. I think the problem is that you are calling getTweets()
from draw()
, so it get called 60 times each second (default). Inside getTweets()
you creates configurations builders and tweeter streams one each cycle, thats a big batch of them. No need for that. the "stream" once opened is "listening" to all twitters (not really...) for your query, and will call the method onStatus()
for every tweet that matches it. So no need to call this in draw. Just opens the stream and... that's it :). Then in StatusListener interface you implements what you need to be done with the tweets, and every new one will go through that path. As you did. Just place the interface in the code. No need to call it in draw. It works more or less like the mousePressed()
method, it get called each time you press the button. This one, on every matching Status. Cool. So move instantiation to setup()
. Here i got it running for like 10 minutes with no problem...
看(改编的代码):
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
这篇关于处理 twitter4j 没有文件添加到草图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!