GetRecentPostsOperation.java

/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */ 

package com.nokia.example.wordpress.networking;

import java.util.Vector;

/**
 * Retrieves posts from a specified blog.
 */
public class GetRecentPostsOperation extends NetworkOperation {

    /**
     *  Listener interface
     */
    public interface Listener {

        /**
         * Returns the retrieved posts, or null in case of an error.
         * @param posts
         */
        public void postsReceived(Vector posts);
    }
    private static final String WP_GET_RECENT_POSTS = "metaWeblog.getRecentPosts";
    private Blog blog;
    private Listener listener;

    /**
     * Constructor
     * @param listener
     * @param blog Blog to load the posts from.
     */
    public GetRecentPostsOperation(Listener listener, Blog blog) {
        this.listener = listener;
        this.blog = blog;
    }

    /**
     * Starts the operation.
     */
    public void start() {
        Request req = new Request(WP_GET_RECENT_POSTS);
        req.addStringParam(blog.blogId);
        req.addStringParam(blog.blogUser);
        req.addStringParam(blog.blogUserPassword);
        req.addIntParam("50"); // how many posts to get
        Network nw = new Network(this);
        nw.startHttpPost(blog.xmlrpcUrl, req.request());
    }

    /**
     * Parses the server response and calls the listener.
     * @param response
     */
    public void networkHttpPostResponse(String response) {
      
        try {
            PostsParser handler = new PostsParser(blog);
            parse(response, handler);
            listener.postsReceived(handler.posts);
        } catch (ParseError e) {
            listener.postsReceived(null);
        }
    }
}