Getting posts from WordPress in my android app

What you want to do is to create some kind of REST API from your WordPress to return JSON responses to your Android HTTP requests. To do that, first for the Android, you may refer to this post:

Make an HTTP request with android

Then, for the server side (your WordPress) you will have to add a plugin to handle your API requests. To do so, create a file called api-endpoint.php inside your wp-content/plugins and use something like this:

<?php

class API_Endpoint{

 /** Hook WordPress
 *  @return void
 */
 public function __construct(){
    //Ensure the $wp_rewrite global is loaded

    add_filter('query_vars', array($this, 'add_query_vars'), 0);
    add_action('parse_request', array($this, 'sniff_requests'), 0);
    add_action('init', array($this, 'add_endpoints'), 0);
}   

  /**
      * Add public query vars
  * @param array $vars List of current public query vars
  * @return array $vars 
  */
 public function add_query_vars($vars){
    $vars[] = '__api';
    return $vars;
 }

 /** 
     * Add API Endpoints
 *  Regex for rewrites - these are all your endpoints
 *  @return void
 */
 public function add_endpoints(){
    //Get videos by category - as an example
    add_rewrite_rule('^api/videos/?([0-9]+)?/?','index.php?__api=1&videos_cat=$matches[1]','top');

    //Get products - as an example
    add_rewrite_rule('^api/product/?([0-9]+)?/?','index.php?__api=1&product=$matches[1]','top');
 }

  /**   Sniff Requests
  * This is where we hijack all API requests
  *     If $_GET['__api'] is set, we kill WP and serve up rss
  * @return die if API request
  */
 public function sniff_requests(){
    global $wp;

    if(isset($wp->query_vars['__api'])){
        $this->handle_api_request();
        exit;
    }

 }

/** Handle API Requests
 *  This is where we handle off API requests
 *  and return proper responses
 *  @return void
 */
 protected function handle_api_request(){
    global $wp;
    /**
    *
    * Do your magic here ie: fetch from DB etc
    * then get your $result
    */

    $this->send_response($result);
 }



 /** Response Handler
 *  This sends a JSON response to the browser
 */
 protected function send_response(array $data){
    header('content-type: application/json; charset=utf-8');
    echo json_encode($data);
    exit;
 }



}
new API_Endpoint();

Then enable the API_Endpoint plugin through your WordPress admin interface and don't forget to flush your permalinks.

After that you'll be able to make API requests to:

http://example.com/api/videos/12

or

http://example.com/api/product/4

Edit

To get WordPress categories for example reference here - http://codex.wordpress.org/Function_Reference/get_categories


Thanks Gregra for helping me. I found a solution. I installed WordPress plugin JSON API in wordpress site and referred this link http://www.learn2crack.com/2013/10/android-json-parsing-url-example.html to code in Android App


In case of sending back data from wordpress to android app, using the JSON APi Plugin is very bad, although it give expected results but did you actually see what is the result of the json query??

Just check it, enter in your browser: www.yourwebsite.com/api/get_posts/

and check what you will get, this will query all posts or the number of posts you set to default in your wordpress dashboard, and will send all data about them as a json string, I tried to query 10 posts and the size of the json string was about 150KB imagine, just 10 posts, the user will have to download all them every time to get just 10 posts.

Solution: You should query the posts on server side and send back to android app only the data you are going to use, ex: title, thumbnail, excerpt ....

How to do so?

1- Make a php file in your wordpress dir and make it accessible

2- receive in it the posted values from android (these will be set by you in android to know the type of query you want, like number of posts and what post type ...)

3- query posts using wordpress functions according to the query entites in part-2

4- Generate your own json string with only the data you want to use.

5- echo the json string back to android

Now if I want only the title and the thumbnail link of 10 posts, the json string size will be about 2KB

that makes a difference :-)

You can use the JSON API Auth to register and login user it is easy/fast to implement and use.