Database Reference
In-Depth Information
Fig. 2.3 An example of a
Twitter network with different
types of edges
Alice
John
Peter
Bob
Listing 2.4
Using the Twitter API to fetch the followers of a user
public JSONArray GetFollowers(String username) {
...
// Step 1: Create the API request using the supplied
username
URL url = new URL( "https://api.twitter.com/1.1/followers
/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( "/
followers/list" ));
} catch (InterruptedException ex) {
Logger.getLogger(RESTApiExample. class .
getName()).log(Level.SEVERE, null,
ex);
}
}
// Step 4: Retrieve the followers list from Twitter
bRead = new BufferedReader(new InputStreamReader((
InputStream) huc.getContent()));
StringBuilder content = new StringBuilder();
String temp = "" ;
while((temp = bRead.readLine())!=null) {
content.append(temp);
}
try {
 
Search WWH ::




Custom Search