Getting Posts from WordPress in an Android App

I'm new to Android development, and I'm trying to create an application that will simply display categories of posts and posts from the WordPress site. Can anybody help me.

+8
android wordpress
source share
6 answers

Thank you Gregra for your help. I have found a solution. I installed the WordPress JSON API plugin on the wordpress website and typed this link http://www.learn2crack.com/2013/10/android-json-parsing-url-example.html for the code in the Android application

+8
source share

What you want to do is create some kind of REST API from your WordPress in order to return JSON responses to your HTTP requests for Android. For this, first for Android, you can refer to this post:

Make an HTTP request using android

Then for the back end (your WordPress) you will have to add a plugin to handle your API requests. To do this, create the api-endpoint.php file 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 the WordPress admin interface and remember to clear your permalinks.

After that, you can make API requests:

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

or

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

Edit

To get WordPress categories, for example, the link here is http://codex.wordpress.org/Function_Reference/get_categories

+8
source share

In the case of sending back data from Wordpress to an Android application using JSON, APi Plugin is very bad , although it gives the expected results, but have you really seen what the result of the json request is?

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

and check what you get, this will request all the messages or the number of messages that you set by default in your Wordpress panel and send all the data about them as a json string, I tried to request 10 messages and the size from the json string was about 150 KB to submit a total of 10 messages, the user will have to download all of them each time to receive a total of 10 messages.

Decision. You should request messages on the server side and send back to the Android application only the data that you are going to use, for example: name, thumbnail, shutter speed ....

How to do it?

1- Make php file in your wordpress directory and make it available

2- receive published values ​​from android in it (they will be set by you in android to find out the type of the requested request, for example, the number of messages and the type of messages ...)

3 message-messages using wordpress functions in accordance with entites request in part-2

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

5 repeat json string back to android

Now, if I only need a title and a thumbnail of 10 posts, the json string size will be approximately 2KB

what matters :-)

You can use the Auth JSON API to register and login, which is easy or fast to implement and use.

+2
source share

I think this is better, to use wordpress rest api you need to use Wordpress 4.7 or higher or install the Rest Api plugin in previous versions. Then you need to configure Permalinks in wordpress, this will make api rest endpoints work.

To reduce the size and configuration on the json output, you can install the Rest api filter plugin, see the following example:

Receiving a specified number of messages

To get a given number of messages, you can use the filter after each page. Only 3 posts will be displayed in the URL below. http: // your-blog-url / wp-json / wp / v2 / posts? filter [posts_per_page] = 3

Receive a special message

You can get any specific record by its identifier. http: // your-blog-url / wp-json / wp / v2 / posts / 67

Here 67 is the message identifier.

Field filtering

As you saw in the JSON data, there are several fields that we do not need. Thus, using the REST API - Filter Fields plugin, you can filter several fields. For example, you want to receive only the id and title messages, then this can be done using the following URL. http: // your-blog-url / wp-json / wp / v2 / posts? fields = id, title

+1
source share

First you need to install the WordPress Rest API v2 on your Wordpress. You can get information about all the posts on your blog at the following URL. It will return a JSON response containing all the information about your blog.

 http://your-blog-url/wp-json/wp/v2/posts for example http://www.blueappsoftware.in/android/wp-json/wp/v2/posts 

Now you can call this url from android using retrofit / volley / httpconnection. I suggest you use a modification. You can create your own user interface for Android on the blog display. You can get the link from here - http://www.blueappsoftware.in/android/blog/get-wordpress-post-in-android-app/

0
source share

First, you must install the WordPress Rest API v2 on your WordPress. You can get information about all the posts on your blog at the following URL. It will return a JSON response containing all the information about your blog.

-2
source share

All Articles