import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder;
public class GetUserStatus {
public static void main(String[] args) {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey("XXXXXXXXXXXXXXXXXXXXX");
cb.setOAuthConsumerSecret("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
cb.setOAuthAccessToken("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
cb.setOAuthAccessTokenSecret("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
TwitterStream twitterStream = new TwitterObjectFactory(cb.build()).getInstance();
UserStreamListener listener = new UserStreamListener() {
public void onStatus(Status status) {
System.out.println("onStatus @" + status.getUser().getScreenName() + " - " + status.getText());
}
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
//System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
}
public void onDeletionNotice(long directMessageId, long userId) {
// System.out.println("Got a direct message deletion notice id:" + directMessageId);
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
// System.out.println("Got a track limitation notice:" + numberOfLimitedStatuses);
}
public void onScrubGeo(long userId, long upToStatusId) {
// System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}
// public void onStallWarning(StallWarning warning) {
// // System.out.println("Got stall warning:" + warning);
// }
public void onFriendList(long[] friendIds) {
/*System.out.print("onFriendList");
for (long friendId : friendIds) {
System.out.print(" " + friendId);
}
System.out.println();*/
}
public void onFavorite(User source, User target, Status favoritedStatus) {
/* System.out.println("onFavorite source:@"
+ source.getScreenName() + " target:@"
+ target.getScreenName() + " @"
+ favoritedStatus.getUser().getScreenName() + " - "
+ favoritedStatus.getText());*/
}
public void onUnfavorite(User source, User target, Status unfavoritedStatus) {
/*System.out.println("onUnFavorite source:@"
+ source.getScreenName() + " target:@"
+ target.getScreenName() + " @"
+ unfavoritedStatus.getUser().getScreenName()
+ " - " + unfavoritedStatus.getText());*/
}
public void onFollow(User source, User followedUser) {
/*System.out.println("onFollow source:@"
+ source.getScreenName() + " target:@"
+ followedUser.getScreenName());*/
}
public void onDirectMessage(DirectMessage directMessage) {
/* System.out.println("onDirectMessage text:"
+ directMessage.getText());*/
}
public void onUserListMemberAddition(User addedMember, User listOwner, UserList list) {
/*System.out.println("onUserListMemberAddition added member:@"
+ addedMember.getScreenName()
+ " listOwner:@" + listOwner.getScreenName()
+ " list:" + list.getName());*/
}
public void onUserListMemberDeletion(User deletedMember, User listOwner, UserList list) {
/* System.out.println("onUserListMemberDeleted deleted member:@"
+ deletedMember.getScreenName()
+ " listOwner:@" + listOwner.getScreenName()
+ " list:" + list.getName());*/
}
public void onUserListSubscription(User subscriber, User listOwner, UserList list) {
/*System.out.println("onUserListSubscribed subscriber:@"
+ subscriber.getScreenName()
+ " listOwner:@" + listOwner.getScreenName()
+ " list:" + list.getName());*/
}
public void onUserListUnsubscription(User subscriber, User listOwner, UserList list) {
/*System.out.println("onUserListUnsubscribed subscriber:@"
+ subscriber.getScreenName()
+ " listOwner:@" + listOwner.getScreenName()
+ " list:" + list.getName());*/
}
public void onUserListCreation(User listOwner, UserList list) {
/*System.out.println("onUserListCreated listOwner:@"
+ listOwner.getScreenName()
+ " list:" + list.getName());*/
}
public void onUserListUpdate(User listOwner, UserList list) {
/*System.out.println("onUserListUpdated listOwner:@"
+ listOwner.getScreenName()
+ " list:" + list.getName());*/
}
public void onUserListDeletion(User listOwner, UserList list) {
/*System.out.println("onUserListDestroyed listOwner:@"
+ listOwner.getScreenName()
+ " list:" + list.getName());*/
}
public void onUserProfileUpdate(User updatedUser) {
//System.out.println("onUserProfileUpdated user:@" + updatedUser.getScreenName());
}
public void onBlock(User source, User blockedUser) {
/*System.out.println("onBlock source:@" + source.getScreenName()
+ " target:@" + blockedUser.getScreenName());*/
}
public void onUnblock(User source, User unblockedUser) {
/*System.out.println("onUnblock source:@" + source.getScreenName()
+ " target:@" + unblockedUser.getScreenName());*/
}
public void onException(Exception ex) {
/* ex.printStackTrace();
System.out.println("onException:" + ex.getMessage());*/
}
};
String useIds[] = {"99901654"};
twitterStream.addListener(listener);
twitterStream.user(useIds);
}
}
错误:
UserStreamListener cannot be resolved to a type GetUserStatus.java /TwitterApi/src line 18 Java Problem
UserStreamListener cannot be resolved to a type GetUserStatus.java /TwitterApi/src line 18 Java Problem
TwitterStream cannot be resolved to a type GetUserStatus.java /TwitterApi/src line 16 Java Problem
The method getInstance() is undefined for the type TwitterObjectFactory GetUserStatus.java /TwitterApi/src line 16 Java Problem
The constructor TwitterObjectFactory(Configuration) is undefined GetUserStatus.java /TwitterApi/src line 16 Java Problem
答案 0 :(得分:1)
您正在尝试获取流,但您的配置错误,可能您的代码来自旧版本的Twitter4j。这一行:
TwitterStream twitterStream = new TwitterObjectFactory(cb.build()).getInstance();
应该是
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
并且
UserStreamListener listener = new UserStreamListener(){
应该是
StatusListener listener = new StatusListener() {
@Override
public void onStallWarning(StallWarning arg0) {
// TODO Auto-generated method stub
}
使用onStallWarning方法。您可以查看此页Twitter4j - PrintSampleStream.java
我只是尝试使用更改的代码,它可以工作(: