Database Reference
In-Depth Information
Listing 2.10
Searching for Tweets using the REST API
public JSONArray GetSearchResults(String query) {
try {
// Step 1:
String URL_PARAM_SEPERATOR = "&" ;
StringBuilder url = new StringBuilder();
url.append( "https://api.twitter.com/1.1/search/tweets.
json?q=" );
//query needs to be encoded
url.append(URLEncoder.encode(query, "UTF-8" ));
url.append(URL_PARAM_SEPERATOR);
url.append( "count=100" );
URL navurl = new URL(url.toString());
HttpURLConnection huc = (HttpURLConnection) navurl.
openConnection();
huc.setReadTimeout(5000);
Consumer.sign(huc);
huc.connect();
...
// Step 2: Read the retrieved search results
BufferedReader bRead = new BufferedReader(new
InputStreamReader((InputStream) huc.getInputStream()
));
String temp;
StringBuilder page = new StringBuilder();
while( (temp = bRead.readLine())!=null) {
page.append(temp);
}
JSONTokener jsonTokener = new JSONTokener(page.toString
());
try{
JSONObject json = new JSONObject(jsonTokener);
//Step 4: Extract the Tweet objects as an array
JSONArray results = json.getJSONArray( "statuses" );
return results;
...
}
Source: Chapter2/restapi/RESTApiExample.java
Requests to the API can be made using the method GetSearchResults presented in
Listing 2.10 . Input to the function is a keyword or a list of keywords in the form of
an OR query. The function returns an array of Tweet objects.
Key Parameters : result_type parameter can be used to select between the
top ranked Tweets, the latest Tweets, or a combination of the two types of search
results matching the query. The parameters max_id and since_id can be used
to paginate through the results, as in the previous API discussions.
Rate Limit : An application can make a total of 450 requests and up to 180
requests from a single authenticated user within a rate limit window.
 
Search WWH ::




Custom Search