Im getting this error for processing, when I use Twitter4j 3 and processing 2.
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.
Im running processing on a MacOSX.
EDIT****
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?
Thanks in advance!
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...
look (adapted code) :
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