1

我正在使用 twitter4j v2.2.6 的流 API 并且有一些过滤问题。我可以很好地获取示例流,但是当我尝试添加地理位置过滤器时,流输出不会改变(我仍然收到没有地理位置和 geoloc 但在我的边界框之外的推文)。这是我的一些代码:

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();        
}

那么,我做错了什么?有任何想法吗?

问候,

蒂姆

4

1 回答 1

1

删除以下行,然后重试。

twitterStream.sample();   
于 2013-03-14T20:43:19.693 回答