Database Reference
In-Depth Information
Listing 2.5
Using the Twitter API to fetch the friends of a user
public JSONArray GetFriends(String username) {
...
JSONArray friends = new JSONArray();
// Step 1: Create the API request using the supplied
username
URL url = new URL( "https://api.twitter.com/1.1/friends/
list.json?screen_name=" +username+ "&cursor=" +cursor);
HttpURLConnection huc = (HttpURLConnection) url.
openConnection();
huc.setReadTimeout(5000);
// Step 2: Sign the request using the OAuth Secret
Consumer.sign(huc);
huc.connect();
...
/ ** Step 3: If the requests have been exhausted,
* then wait until the quota is renewed
* /
if(huc.getResponseCode()==429) {
try {
Thread.sleep( this .GetWaitTime( "/friends/
list" ));
} catch (InterruptedException ex) {
Logger.getLogger(RESTApiExample. class .
getName()).log(Level.SEVERE, null,
ex);
}
}
// Step 4: Retrieve the friends list from Twitter
bRead = new BufferedReader(new InputStreamReader((
InputStream) huc.getContent()));
...
JSONObject jobj = new JSONObject(content.toString());
// Step 5: Retrieve the token for the next request
cursor = jobj.getLong( "next_cursor" );
JSONArray userlist = jobj.getJSONArray( "users" );
for(int i=0;i<userlist.length();i++) {
friends.put(userlist.get(i));
}
...
return friends;
}
Source: Chapter2/restapi/RESTApiExample.java
Rate Limit : A maximum of 15 calls from a user and 30 API calls from an
application are allowed within a rate limit window.
 
Search WWH ::




Custom Search