1
我使用twitter4j v2.2.6的流API,並有一些過濾問題。我可以獲得樣本流,但是當我嘗試添加地理位置過濾器時,流輸出不會改變(我仍然收到沒有地理位置和geoloc但位於我的邊框外的推文)。這裏是我的一些代碼:Twitter4j v2.2.6流式API與地理定位
private static FilterQuery getBoundingBoxFilter() {
// New Delhi India
double lat = 28.6;
double lon = 77.2;
double lon1 = lon - .5;
double lon2 = lon + .5;
double lat1 = lat - .5;
double lat2 = lat + .5;
double bbox[][] = {{lon1, lat1}, {lon2, lat2}};
FilterQuery filtro = new FilterQuery();
return filtro.locations(bbox);
}
public static void streamIt() {
TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
FilterQuery fq = getBoundingBoxFilter();
StatusListener listener = new StatusListener() {
@Override
public void onStatus(Status status) {
GeoLocation gl = status.getGeoLocation();
StringBuilder msg = new StringBuilder();
if (gl != null) {
msg.append("Lat/Lon: ").append(gl.getLatitude()).append(",").append(gl.getLongitude()).append(" - ");
msg.append(status.getText());
LOG.info(msg.toString());
} else {
msg.append(status.getText());
LOG.info(msg.toString());
}
}
@Override
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
}
@Override
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
}
@Override
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}
@Override
public void onException(Exception ex) {
if (!(ex instanceof IllegalStateException)) {
ex.printStackTrace();
}
}
@Override
public void onStallWarning(StallWarning sw) {
throw new UnsupportedOperationException("Not supported yet.");
}
};
twitterStream.addListener(listener);
if (fq != null) {
twitterStream.filter(fq);
}
twitterStream.sample();
}
那麼,我在做什麼錯了?有任何想法嗎?
問候,
添