Twitter4j v2.2.6带有地理位置的流式API

时间:2013-03-11 19:58:15

标签: java api geolocation streaming twitter4j

我正在使用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();        
}

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

此致

1 个答案:

答案 0 :(得分:1)

删除以下行并重试。

twitterStream.sample();