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;Create an entry instance
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;
Use Blogger API to create an entry that simulates an actual blog post.
Entry myEntry = new Entry();Get reference to categories of the entry instance
myEntry.setTitle(new PlainTextConstruct("some title"));
myEntry.setContent(new HtmlTextConstruct("some HTML body"));
// set whatever further attributes you want...
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();Add the category to the categories reference
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);
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)Questions? Let me know!
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!
}
0 komentar:
Posting Komentar