12.7.10

How 2 add labels to your blogpost with Java

import java.io.IOException;
import java.net.URL;
import java.util.Set;

import com.google.gdata.client.GoogleService;
import com.google.gdata.client.blogger.BloggerService;
import com.google.gdata.data.Category;
import com.google.gdata.data.Entry;
import com.google.gdata.data.Feed;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.util.ServiceException;

public class MaxBlogPost {

        private static final String METAFEED_URL = "http://www.blogger.com/
feeds/default/blogs";

        public static Entry createPost(GoogleService myService, String
blogID, String title, String content, boolean bIsDraft, String labels)
throws ServiceException, IOException {
                Entry newPost = new Entry();
                newPost.setTitle(new PlainTextConstruct(title));
                newPost.setContent(new PlainTextConstruct(content));
                Set<Category> categories = newPost.getCategories();
                categories.clear();
                String schema = "http://www.blogger.com/atom/ns#";
                categories.add(new Category(schema, labels));
                newPost.setDraft(bIsDraft);
                URL postUrl = new URL("http://www.blogger.com/feeds/" + blogID + "/
posts/default");
                return myService.insert(postUrl, newPost);
        }

        public static void main(String[] args) {
                String applicationName = "MaxRecorder";
                String userName = "<youremail>@gmail.com";
                String password = "xxxxxx";
                BloggerService myService = new BloggerService(applicationName);
                String title = "De Mortel";
                String content = getMyPlayerContent("http://www.youtube.com/cp/
vjVQa1PpcFNtmdXWIXjmKrUfnG2ljl1inDONsteF3ts=");
                try {
                        myService.setUserCredentials(userName, password);
                        String blogID = getBlogId(myService);
                        String labels = "De Mortel, 2010, Alticom, falcon peregrines,
Internl.net, slechtvalken, wildlife, slechtvalken.startkabel.nl,
alticom.nl";
                        @SuppressWarnings("unused")
                        Entry blogEntry = createPost(myService, blogID, title, content,
true, labels);
                } catch (ServiceException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }

        private static String getBlogId(BloggerService myService) throws
ServiceException, IOException {
                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!");
        }

        public static String getMyPlayerContent(String playerUrl) {
                return "<object width='640' height='385'><param name='movie'
value='" + playerUrl + "'></param><embed src='" + playerUrl + "'
type='application/x-shockwave-flash' width='640' height='385'></
embed></object>";
        }

}

http://groups.google.com/group/bloggerdev/browse_thread/thread/d2869b0754b78d8e

No comments: