Java Reference
In-Depth Information
System.out.println("----- Get user time line -----");
jsonResponse = client.makeRequest("statuses/home_timeline.json");
System.out.println(jsonResponse);
System.out.println("----- Get user time line: 5 tweets -----");
jsonResponse = client.makeRequest("statuses/home_timeline.json",
new HashMap<String, String>() {{
put("count", "5");
}});
System.out.println(jsonResponse);
System.out.println("----- Get user WileyTech's timeline: 5 tweets -----");
jsonResponse = client.makeRequest("statuses/user_timeline.json",
new HashMap<String, String>() {{
put("screen_name", "WileyTech");
put("count", "5");
}});
System.out.println(jsonResponse);
System.out.println("----- Post a tweet -----");
jsonResponse = client.makeRequest("statuses/update.json", "POST",
new HashMap<String, String>() {{
put("status", "Posting this Tweet from Java!" +
" [" + System.currentTimeMillis() + "]");
}});
System.out.println(jsonResponse);
}
}
Again, don't forget to fill in your API keys. If all goes right, the first time you execute this code a
browser window will open and ask you to authorize the app. Enter the PIN in the console, and the
remainder of the code will execute. Try executing this code again, and the stored access code will be
used:
----- Get user time line -----
[** HUGE JSON RESPONSE **]
----- Get user time line: 5 tweets -----
[** SHORTER JSON RESPONSE **]
----- Get user WileyTech's timeline: 5 tweets -----
[** DIFFERENT SET OF TWEETS**]
----- Post a tweet -----
{** JSON CONTAINING POSTED TWEET **}
Check your Twitter page. Notice that your tweet has been posted (a timestamp was added because
Twitter does not allow you to post the same tweet again). See Figure 10-23.
If you're getting errors, it might be due to the following:
Your access token might have been revoked. Try removing the text files under the tokens
folder (you might need to refresh this folder in Eclipse) and trying again.
Search WWH ::




Custom Search