java - 按位置过滤时使用 hbc 的 fatal error 代码 406

标签 java twitter twitter-streaming-api twitter-hbc

我正在尝试使用 Twitter hbc library根据位置获取推文。我已经根据示例文件 FilterStreamExample 设置了我的代码。该示例有效,但当我尝试向端点添加位置时,收到 fatal error 代码:406。

这是有效的代码:

StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
endpoint.trackTerms(Lists.newArrayList("twitterapi", "#yolo"));

但是当我添加以下代码时,出现错误:

Coordinate southwest = new Coordinate(-87.648926, 41.883109);
Coordinate northeast = new Coordinate(-87.606354, 41.828050);
Location chicago = new Location(southwest, northeast); 
endpoint.locations(Lists.newArrayList(chicago));

感谢您的帮助!

格雷格

编辑 这是我的完整代码:

public class App {

  public static void oauth(String consumerKey, String consumerSecret, String token, String secret) throws InterruptedException {
    BlockingQueue<String> queue = new LinkedBlockingQueue<String>(100000);

    StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
    Coordinate southwest = new Coordinate(-87.857666, 41.657347);
    Coordinate northeast = new Coordinate(-87.587128, 41.795712);
    Location chicago = new Location(southwest, northeast); 
    endpoint.locations(Lists.newArrayList(chicago));

    Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);

    BasicClient client = new ClientBuilder()
      .name("exampleStream")
      .hosts(Constants.STREAM_HOST)
      .endpoint(endpoint)
      .authentication(auth)
      .processor(new StringDelimitedProcessor(queue))
      .build();

    // Establish a connection
    client.connect();
    PrintWriter writer;
    try {
      writer = new PrintWriter("twitterstream.txt", "UTF-8"); 
// Do whatever needs to be done with messages
      for (int msgRead = 0; msgRead < 1000; msgRead++) {
        if (client.isDone()) {
          System.out.println("Client connection closed unexpectedly: " + client.getExitEvent().getMessage());
          break;
      }
      String msg = queue.poll(5, TimeUnit.SECONDS);
      if (msg == null) {
        System.out.println("Did not receive a message in 5 seconds");
      } else {
        writer.println(msg);
      }
    }
     writer.close();
        } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
      e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
      e.printStackTrace();
        }  

        client.stop();
}

  public static void main(String[] args) {
  try {
      App.oauth("LEDrJPDhn6C2trbde95WZg", "gEmlgewUodOoDjMNl2QbcqsbGvYDxNZWNRULchPE", "292576546-SWzEgMU7mC7Haat3lLEOQ0mXsZFwBf8F3ZKLBMVa", "ajPN2rGLx57yPoUjYe2qN4bE763orux4KwYeoCAvuLRPM");
  } catch (InterruptedException e) {
      System.out.println(e);
  }

}

最佳答案

也许问题出在坐标上。您应该在 http://boundingbox.klokantech.com/ 上进行验证。之后选择选项“csv”来分隔位置。

关于java - 按位置过滤时使用 hbc 的 fatal error 代码 406,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19774320/

相关文章:

java - 获取异常 java.util.NoSuchElementException : key not found: 'text' in spark-cassandra-connector

Twitter 流 API - 尝试根据位置/边界框获取结果

java - 如何部署 OSGi 应用程序和依赖项?

python-2.7 - 如何使用 tweepy 检索当前未关注的关注者数量?

c# - 在 url 和参数中传递逗号 (,) 时,Twitter 流式传输 api 返回未授权

iphone - 如何在基于 iPhone View 的应用程序中使用 MGTwiiterEngine?

twitter - 特威松 : get followers list

java - ORA-01722 : invalid number when using Hibernate

java - Android应用程序中的JSON解析

java - Android Gradle:未找到插件ID Java库