if (feedUrlString == null) {
throw new IllegalArgumentException("Must specify feed URL");
}
GoogleService service = new GoogleService(serviceName, appName);
try {
// URL of service endpoint.
URL feedUrl = new URL(feedUrlString);
// Set up authentication.
if (username != null) {
if (password == null) {
throw new IllegalArgumentException("Must specify password");
}
service.setUserCredentials(username, password);
}
// Send the query request and receive the response.
Feed feed = service.getFeed(feedUrl, Feed.class);
// Print the title and update time of the returned feed.
System.out.println("Feed title " + feed.getTitle().getPlainText() +
" (" + feed.getUpdated() + ")");
// Print the title and update time and body of each entry.
System.out.println("Entries:");
for (Entry e : feed.getEntries()) {
String content =
(e.getContent() != null ?
((TextContent) e.getContent()).getContent().getPlainText() :
"");
System.out.println(" " + e.getTitle().getPlainText() +
" (" + e.getUpdated() + ")" +
(content.length() > 0 ? ": " : "") + content);
}
// Insert, update, and delete an entry if so requested.
if (updateEntry) {
BaseEntry newEntry = new Entry();
newEntry.setTitle(new PlainTextConstruct("Sample entry title"));
newEntry.setContent(new PlainTextConstruct("Sample entry content"));
BaseEntry e = service.insert(feedUrl, newEntry);
System.out.println("Inserted an entry, ID is " + e.getId());
e.setContent(new PlainTextConstruct("New sample entry content"));
service.update(new URL(e.getEditLink().getHref()), e);
System.out.println("Updated the entry");
service.delete(new URL(e.getEditLink().getHref()));
System.out.println("Deleted the entry");
}
} catch (ServiceException e) {
throw new RuntimeException(e.getMessage() + "\n" + e.getResponseBody());