⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bloggerclient.java

📁 google的gdata api包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Copyright (c) 2006 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package sample.blogger;import com.google.gdata.client.GoogleService;import com.google.gdata.client.Query;import sample.util.SimpleCommandLineParser;import com.google.gdata.data.DateTime;import com.google.gdata.data.Entry;import com.google.gdata.data.Feed;import com.google.gdata.data.Person;import com.google.gdata.data.PlainTextConstruct;import com.google.gdata.data.TextContent;import com.google.gdata.util.ServiceException;import java.io.IOException;import java.net.URL;/** * Demonstrates how to use the Google Data API's Java client library to * interface with the Blogger service. There are examples for the following * operations: *  * <ol> * <li>Retrieving the list of all the user's blogs</li> * <li>Retrieving all posts on a single blog</li> * <li>Performing a date-range query for posts on a blog</li> * <li>Creating draft posts and publishing posts</li> * <li>Updating posts</li> * <li>Retrieving comments</li> * <li>Deleting posts</li> * </ol> *  *  */public class BloggerClient {  private static final String METAFEED_URL =       "http://www.blogger.com/feeds/default/blogs";  private static final String FEED_URI_BASE = "http://www.blogger.com/feeds";  private static final String POSTS_FEED_URI_SUFFIX = "/posts/default";  private static final String COMMENTS_FEED_URI_SUFFIX = "/comments/default";  private static String feedUri;  /**   * Utility classes should not have a public or default constructor.   */  private BloggerClient() {    // do nothing  }  /**   * Parses the metafeed to get the blog ID for the authenticated user's default   * blog.   *    * @param myService An authenticated GoogleService object.   * @return A String representation of the blog's ID.   * @throws ServiceException If the service is unable to handle the request.   * @throws IOException If the URL is malformed.   */  private static String getBlogId(GoogleService myService)      throws ServiceException, IOException {    // Get the metafeed    final URL feedUrl = new URL(METAFEED_URL);    Feed resultFeed = myService.getFeed(feedUrl, Feed.class);    // If the user has a blog then return the id (which comes after 'blog-')    if (resultFeed.getEntries().size() > 0) {      Entry entry = resultFeed.getEntries().get(0);      return entry.getId().split("blog-")[1];    }    throw new IOException("User has no blogs!");  }  /**   * Prints a list of all the user's blogs.   *    * @param myService An authenticated GoogleService object.   * @throws ServiceException If the service is unable to handle the request.   * @throws IOException If the URL is malformed.   */  public static void printUserBlogs(GoogleService myService)      throws ServiceException, IOException {    // Request the feed    final URL feedUrl = new URL(METAFEED_URL);    Feed resultFeed = myService.getFeed(feedUrl, Feed.class);    // Print the results    System.out.println(resultFeed.getTitle().getPlainText());    for (int i = 0; i < resultFeed.getEntries().size(); i++) {      Entry entry = resultFeed.getEntries().get(i);      System.out.println("\t" + entry.getTitle().getPlainText());    }    System.out.println();  }  /**   * Creates a new post on a blog. The new post can be stored as a draft or   * published based on the value of the isDraft paramter. The method creates an   * Entry for the new post using the title, content, authorName and isDraft   * parameters. Then it uses the given GoogleService to insert the new post. If   * the insertion is successful, the added post will be returned.   *    * @param myService An authenticated GoogleService object.   * @param title Text for the title of the post to create.   * @param content Text for the content of the post to create.   * @param authorName Display name of the author of the post.   * @param userName username of the author of the post.   * @param isDraft True to save the post as a draft, False to publish the post.   * @return An Entry containing the newly-created post.   * @throws ServiceException If the service is unable to handle the request.   * @throws IOException If the URL is malformed.   */  public static Entry createPost(GoogleService myService, String title,      String content, String authorName, String userName, Boolean isDraft)      throws ServiceException, IOException {    // Create the entry to insert    Entry myEntry = new Entry();    myEntry.setTitle(new PlainTextConstruct(title));    myEntry.setContent(new PlainTextConstruct(content));    Person author = new Person(authorName, null, userName);    myEntry.getAuthors().add(author);    myEntry.setDraft(isDraft);    // Ask the service to insert the new entry    URL postUrl = new URL(feedUri + POSTS_FEED_URI_SUFFIX);    return myService.insert(postUrl, myEntry);  }  /**   * Displays the titles of all the posts in a blog. First it requests the posts   * feed for the blogs and then is prints the results.   *    * @param myService An authenticated GoogleService object.   * @throws ServiceException If the service is unable to handle the request.   * @throws IOException If the URL is malformed.   */  public static void printAllPosts(GoogleService myService)      throws ServiceException, IOException {    // Request the feed    URL feedUrl = new URL(feedUri + POSTS_FEED_URI_SUFFIX);    Feed resultFeed = myService.getFeed(feedUrl, Feed.class);    // Print the results    System.out.println(resultFeed.getTitle().getPlainText());    for (int i = 0; i < resultFeed.getEntries().size(); i++) {      Entry entry = resultFeed.getEntries().get(i);      System.out.println("\t" + entry.getTitle().getPlainText());    }    System.out.println();  }  /**   * Displays the title and modification time for any posts that have been   * created or updated in the period between the startTime and endTime   * parameters. The method creates the query, submits it to the GoogleService,   * then displays the results.   *    * Note that while the startTime is inclusive, the endTime is exclusive, so   * specifying an endTime of '2007-7-1' will include those posts up until   * 2007-6-30 11:59:59PM.   *    * @param myService An authenticated GoogleService object.   * @param startTime DateTime object specifying the beginning of the search   *        period (inclusive).   * @param endTime DateTime object specifying the end of the search period   *        (exclusive).   * @throws ServiceException If the service is unable to handle the request.   * @throws IOException If the URL is malformed.   */  public static void printDateRangeQueryResults(GoogleService myService,      DateTime startTime, DateTime endTime) throws ServiceException,      IOException {    // Create query and submit a request    URL feedUrl = new URL(feedUri + POSTS_FEED_URI_SUFFIX);    Query myQuery = new Query(feedUrl);    myQuery.setUpdatedMin(startTime);    myQuery.setUpdatedMax(endTime);    Feed resultFeed = myService.query(myQuery, Feed.class);    // Print the results    System.out.println(resultFeed.getTitle().getPlainText() + " posts between "        + startTime + " and " + endTime);    for (int i = 0; i < resultFeed.getEntries().size(); i++) {      Entry entry = resultFeed.getEntries().get(i);      System.out.println("\t" + entry.getTitle().getPlainText());      System.out.println("\t" + entry.getUpdated().toStringRfc822());    }    System.out.println();  }  /**   * Updates the title of the given post. The Entry object is updated with the   * new title, then a request is sent to the GoogleService. If the insertion is   * successful, the updated post will be returned.   *    * Note that other characteristics of the post can also be modified by

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -