Rabu, 23 Juni 2010

Add Labels To A Post Via Google's Java Blogger API

Many bloggers using Google Blogger Java API are wondering how to set the labels of a post via their API, and I found a solution after much trial and error For those who don't know a label is a field of a post where you can put in a comma-delimited list of words that describe this post. In the Blogger admin page it looks like this:

Google Blogger Create A Post Admin Page Labels Field

For example this post you are reading right now is about how to create labels for a post through Blogger's Java API, and it can have "Blogger,Java" as the labels. Doing so in the admin page is easy, but what about doing so programmatically via Blogger API?

Here's what you do:

Import necessary Java Blogger API libraries
They include but not limited to the following:
import com.google.gdata.client.blogger.BloggerService;
import com.google.gdata.data.Entry;
import com.google.gdata.data.Category;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.HtmlTextConstruct;
import java.net.URL;
Create an entry instance
Use Blogger API to create an entry that simulates an actual blog post.
Entry myEntry = new Entry();
myEntry.setTitle(new PlainTextConstruct("some title"));
myEntry.setContent(new HtmlTextConstruct("some HTML body"));
// set whatever further attributes you want...
Get reference to categories of the entry instance
Set categories=myEntry.getCategories();


Create a Category
Create a Category as follows. Note you need to set label and labelLang to null. And you MUST set scheme properly. I would think if I didn't setLabel and setLabelLang they'd be set to null by default, but it turns out that's not true.
Category category= new Category();
category.setTerm("whatever label you like such as Google Blogger or Java API");
category.setLabel(null);
category.setScheme("http://www.blogger.com/atom/ns#");
category.setLabelLang(null);
Add the category to the categories reference
Use the following code to add the category we just created to the reference to categories.

categories.add(category);


To create more categories simply repeat the step above and this step. Do NOT concatenate them with commas and set it via setTerm(). For example suppose you want to add "Java API,Google Blogger" as the labels. You'd create a category for "Java API" (let's call it categoryJavaApi) and a category for "Google Blogger" (let's call it categoryGoogleBlogger) and add them to the categories reference like the following:

categories.add(categoryJavaApi);
categories.add(categoryGoogleBlogger);

Any questions?

Create this post on your blog via API
// suppose your blog ID is 123456
URL postUrl = new URL("http://www.blogger.com/feeds/123456/posts/default");
myService.insert(postUrl, myEntry);


That's it! If you encounter any difficulty don't hesitate to let me know!

By the way some people are wondering how to retrieve all categories of a blog, with duplicates removed. Here's how you do it.

How to retrieve all categories of a blog via Java API
public static void printAllPosts(GoogleService myService, String blogId)
throws ServiceException, IOException {
// Standard way to request the blog given blog ID
URL feedUrl = new URL("http://www.blogger.com/feeds/"
+ blogID + "/posts/default");
Feed resultFeed = myService.getFeed(feedUrl, Feed.class);

// Loop through and put every post's categories in a HashSet
Set categories=new HashSet();
for (int i = 0; i < resultFeed.getEntries().size(); i++) {
Entry entry = resultFeed.getEntries().get(i);
categories.addAll(entry.getCategories());
}

// Now variable 'categories' contains all unique categories!
}
Questions? Let me know!

0 komentar:

 
support by: infomediaku.com