I am new in Android development and I am trying to make an app that will simply display the post categories and posts from a WordPress website. Can any one help me, please.
Leave a Reply
You must be logged in to post a comment.
I am new in Android development and I am trying to make an app that will simply display the post categories and posts from a WordPress website. Can any one help me, please.
You must be logged in to post a comment.
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:
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: http://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.
I think this is better, for using 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 rest api endpoints to work.
For reduce the size and customice the output json you may install the Rest api filter fields plugin see the bellow example:
Fetching Specified Number of Post
For fetching specified number of posts you can use post-per-page filter. The below URL will fetch only 3 posts.
http://your-blog-url/wp-json/wp/v2/posts?filter%5Bposts_per_page%5D=3
Fetching Particular Post
You can fetch any particular post by its id.
http://your-blog-url/wp-json/wp/v2/posts/67
Here 67 is the id of the post.
Filtering Fields
As you have seen in above JSON data that there are several fields that we don’t require. So with the help of REST API – Filter Fields plugin you can filter few fields. For example you want to fetch only post’s id and title then it can be done by using following URL.
http://your-blog-url/wp-json/wp/v2/posts?fields=id,title
First, you have to install WordPress Rest API v2 on your wordpress. You can fetch information about all the post on your blog by the following URL. It will return a JSON response that contains all the information about your blog.
Now you can call this url from android using retrofit / volley / httpconnection. I will suggest you to use retrofit. You can create you own Custom UI design on android to show blog post. You can get reference from here- http://www.blueappsoftware.in/android/blog/get-wordpress-post-in-android-app/