Database Reference
In-Depth Information
"full_name"
:
"Tempe, AZ"
,
//Other place fields
...
},
"user"
:{
//User Information in the form of Twitter user object
...
}
}
Listing 2.7
Using the Twitter API to fetch the Tweets of a user
public JSONArray GetStatuses(String username) {
...
// Step 1: Create the API request using the supplied
username
// Use (max_id-1) to avoid getting redundant Tweets.
url = new URL(
"https://api.twitter.com/1.1/statuses/
user_timeline.json?screen_name="
+ username+
"&
include_rts="
+include_rts+
"&count="
+tweetcount+
"&
max_id="
+(maxid-1));
HttpURLConnection huc = (HttpURLConnection) url.
openConnection();
huc.setReadTimeout(5000);
// Step 2: Sign the request using the OAuth Secret
Consumer.sign(huc);
/
**
Step 3: If the requests have been exhausted,
*
then wait until the quota is renewed
*
/
...
//Step 4: Retrieve the Tweets from Twitter
bRead = new BufferedReader(new InputStreamReader((
InputStream) huc.getInputStream()));
...
for(int i=0;i<statusarr.length();i++) {
JSONObject jobj = statusarr.getJSONObject(i);
statuses.put(jobj);
// Step 5: Get the id of the oldest Tweet ID as
max_id to retrieve the next batch of Tweets
if(!jobj.isNull(
"id"
)) {
maxid = jobj.getLong(
"id"
);
...
return statuses;
}
Source: Chapter2/restapi/RESTApiExample.java