2013-11-04 15 views
2

我試圖使用Twitter hbc library來獲取基於位置的推文。我已經根據示例文件FilterStreamExample設置了我的代碼。這個例子的作品,但是當我嘗試將位置添加到終點,我得到致命錯誤代碼:406致命錯誤代碼406當按位置過濾時使用hbc

下面是工作代碼:

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

}

+0

我運行了你的代碼,Tweets被寫入了這個文件,我沒有看到任何錯誤。錯誤是否會立即發生?你能否將錯誤/堆棧軌道的細節添加到問題中?順便說一句,你應該現在撤銷你的API令牌,現在它們是公開的。 – Jonathan

+0

hm ..看起來也許這是一個憑證的東西(雖然它是奇怪的,它會適用於第一個查詢,而不是第二)...感謝您的幫助,但! – user1801348

回答

0

我檢查你的代碼你和我OUTH參數。與我的工作,它不適用於你的。這是問題

+0

奇怪的是,我可以使用相同的憑據進行其他請求,雖然 – user1801348

1

您是否在使用Google地圖座標? 因爲看起來你應該切換你的座標點。

Coordinate southwest = new Coordinate(41.657347, -87.857666); 
Coordinate northeast = new Coordinate(41.795712, -87.587128); 

您也可以使用HBC快照版本https://github.com/twitter/hbc/issues/19 問候。

2

也許問題是座標。您應該在http://boundingbox.klokantech.com/上進行驗證。之後選擇「csv」選項來分隔地點。

+0

你是對的,我驗證了我試圖用這個網站查找的觀點,並發現我以錯誤的順序使用它們。 –

相關問題