Skip to content

Instantly share code, notes, and snippets.

@torao
Last active December 19, 2015 04:39
Show Gist options
  • Save torao/5898602 to your computer and use it in GitHub Desktop.
Save torao/5898602 to your computer and use it in GitHub Desktop.
How to retrieve raw JSON status from Twitter Sample Stream API using twitter4j. / twitter4j を使用して Twitter Sample API から生の JSON を取得する方法。
import twitter4j._
import twitter4j.conf._
import twitter4j.json._
object Stream extends RawStreamListener {
def main(args:String):Unit = {
val conf = new ConfigurationBuilder()
.setDebugEnabled(true)
.setOAuthConsumerKey("xxxx")
.setOAuthConsumerSecret("xxxx")
.setOAuthAccessToken("xxxx")
.setOAuthAccessTokenSecret("xxxx")
.build()
val stream = new TwitterStreamFactory(conf).getInstance()
stream.addListener(this)
stream.sample()
}
def onMessage(rawString:String):Unit = {
// You can restore Status instance by DataObjectFactory.
// val status = DataObjectFactory.createStatus(rawString)
System.out.println(rawString)
}
def onException(ex:Exception):Unit = {
ex.printStackTrace()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment