2013-11-25 41 views
2

Im新的twitter4j和處理。獲取最新的推文並存儲它twitter4j

我想從單詞列表中獲取最新搜索的單詞。我希望它被存儲在將被查詢的變量中。 這樣:

PossibleWords[] ={"sad","happy","joyful"} 

然後,我會希望有一個字符串變量搜索字符串容納包括從數組中字最新的啾啾鳴叫。例如,如果剛剛發佈了悲傷信息,我想獲取該推文。所以我需要測試數組中的任何單詞是否在最新的推文單詞中。

概念上,我明白這一點,但任何人對此有什麼建議?這在使用twitter4j 3

回答

1

你應該使用Twitter的流API,看代碼塊,我下面粘貼:

 private void GetTweetsByKeywords() 
    { 

     TwitterStream twitterStream = new TwitterStreamFactory(config).getInstance(); 

       StatusListener statusListener = new StatusListener() { 
        private int count = 0; 
        private long originalTweetId = 0; 

        @Override 
         public void onStatus(Status status) { 

         // Here do whatever you want with the status object that is the  
         // tweet you got 

         } //end of the onStatus() 
     }; //end of the listener 

     FilterQuery fq = new FilterQuery(); 

     String keywords[] = {"sad","happy","joyful"}; 

     fq.track(keywords); 


     twitterStream.addListener(statusListener); 
     twitterStream.filter(fq); 
    } 
+0

謝謝你,這是非常感謝,我用這個,但我得到一個錯誤,你能看看它? http://stackoverflow.com/questions/20332659/processing-twitter4j-no-files-added-to-sketch –

0

你有沒有考慮過使用Twitter流媒體API?這允許您傳遞多達400個關鍵字,並獲得包含其中任何單詞的推文的實時流。例如在Twitter上對於j在這裏看到:

twitter4j - access tweet information from Streaming API

替換行

String keywords[] = {"France", "Germany"}; 

String keywords[] = {"sad","happy","joyful"}; 

,你應該得到的鳴叫流。

+0

感謝這個,這是大加讚賞,我都用這個,但我得到一個錯誤,你可以看看在嗎? http://stackoverflow.com/questions/20332659/processing-twitter4j-no-files-added-to-sketch –