Database Reference
In-Depth Information
2.4.2
Streaming API
Specifically, the statuses/filter 9 API provides a constant stream of public Tweets
published by a user. Using the method CreateStreamingConnection summarized in
Listing 2.8 , we can create a POST request to the API and fetch the search results
as a stream. The parameters are added to the request by reading through a list of
userids using the method CreateRequestBody , which is summarized in Listing 2.9 .
Listing 2.8
Using the Streaming API to fetch Tweets
public void CreateStreamingConnection(String baseUrl, String
outFilePath) {
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames
.CONNECTION_TIMEOUT, new Integer(90000));
//Step 1: Initialize OAuth Consumer
OAuthConsumer consumer = new CommonsHttpOAuthConsumer(
OAuthUtils.CONSUMER_KEY,OAuthUtils.CONSUMER_SECRET);
consumer.setTokenWithSecret(OAuthToken.getAccessToken(),
OAuthToken.getAccessSecret());
//Step 2: Create a new HTTP POST request and set
parameters
HttpPost httppost = new HttpPost(baseUrl);
try {
httppost.setEntity(new UrlEncodedFormEntity(
CreateRequestBody(), "UTF-8" ));
...
//Step 3: Sign the request
consumer.sign(httppost);
...
HttpResponse response;
InputStream is = null;
try {
//Step 4: Connect to the API
response = httpClient.execute(httppost);
...
HttpEntity entity = response.getEntity();
try {
is = entity.getContent();
...
//Step 5: Process the incoming Tweet
Stream
this .ProcessTwitterStream(is, outFilePath
);
...
}
Source: Chapter2/streamingapi/StreamingApiExample.java
9 https://dev.twitter.com/docs/api/1.1/post/statuses/filter
 
Search WWH ::




Custom Search