我正在尝试以不同的方法执行单独的查询,下面的代码不是我的实际代码,而只是错误的一个示例,我有
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
两种方法都调用了,但是我得到了错误
IllegalStateException: 不能再使用这个构建器,build() 已经被调用
如果我把它设为全局,那么我会得到同样的错误,这可能只是我很愚蠢,但我无法解决,我现在盯着它太久了。
这是我的代码:
ConfigurationBuilder cb = new ConfigurationBuilder();
double lat = 51.5171;
double lon = 0.1062;
double res = 37;
String resUnit = "mi";
public void setup(){
cb.setOAuthConsumerKey("XXXXXXXXXXXXXXXXXXXXXx");
cb.setOAuthConsumerSecret("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
cb.setOAuthAccessToken("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
cb.setOAuthAccessTokenSecret("XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
queryLon();
queryBir();
}
void queryLon(){
try {
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
for (int j = 0; j < 5; j++) {
Query query = new Query();
query.setRpp(100);
QueryResult result = twitter.search(new Query().geoCode(new GeoLocation(lat,lon), res, resUnit));
ArrayList tweets = (ArrayList) result.getTweets();
for (int i = 0; i < tweets.size(); i++) {
Tweet t = (Tweet) tweets.get(i);
//User u= twitter.showUser("twitter");
String location = t.getLocation();
println("Location: " + location);
}
}
}
catch (TwitterException te) {
println("Couldn't connect: " + te);
};
}
void queryBir(){
try {
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
for (int j = 0; j < 5; j++) {
Query query = new Query();
query.setRpp(100);
QueryResult result = twitter.search(new Query().geoCode(new GeoLocation(lat,lon), res, resUnit));
ArrayList tweets = (ArrayList) result.getTweets();
for (int i = 0; i < tweets.size(); i++) {
Tweet t = (Tweet) tweets.get(i);
String location = t.getLocation();
println("Location: " + location);
}
}
}
catch (TwitterException te) {
println("Couldn't connect: " + te);
};
}
任何帮助将不胜感激,干杯。
忘了提到我使用 Twitter4j 和处理