Hello, guys welcome to WordPress to Android App Tutorial. In past few years WordPress has grown so much. It is the most popular blogging platform as more than 60 million websites are running on it.
WP REST API is one of the best features added to WordPress. It allows developers to create apps that can easily integrate with WordPress platform. Using this API we can easily retrieve our WordPress site’s data like posts, media, users, etc in JSON format by just sending HTTP request.
So in this tutorial I will teach you to make a very basic Android app for WordPress blog or website using WP REST API.
Note: This tutorial is about creating a very basic Android APP using WP REST API. If you are looking for a complete app with all the standard features like, PUSH NOTIFICATION, BOOKMARKS, SOCIAL SHARING etc. Then you can buy it from us. To get the Android App for your WordPress Blog CLICK HERE.
WordPress to Android App using WP REST API Video Demo
You can check this small video clip demonstrating the final output of this WordPress to Android App Tutorial.
Now, if you want to make the same then lets begin our tutorial.
How to work WP REST API?
- To use WP REST API we require following two plugins. So first install these plugins in your wordpress blog.
Installing REST API Plugins
WordPress REST API (Version 2)
- Get this plugin from here -> https://wordpress.org/plugins/rest-api/
- It is the official WP REST API plugin that will be used to fetch data from blog.
REST API – Filter Fields
- Get this plugin from here -> https://wordpress.org/plugins/rest-api-filter-fields/
- The JSON data that we will fetch contains some fields that we don’t require. So using this plugin we can filter the data to decrease its size.
Just go the links that I have given above to download and then install these plugins on your blog.
Fetching All Post of Your Website
- You can fetch information about all the post on your blog by following URL.
http://your-blog-url/wp-json/wp/v2/posts
- For a real example you can see the URL with my blog. Just go to the following link
http://www.thejavaprogrammer.com/wp-json/wp/v2/posts
- You will see the following JSON Data.
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[posts_per_page]=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
- You can also fetch other fields along with id and title by just adding them in the URL. Like if you want to fetch id, title and content then it can be done using following URL.
http://your-blog-url/wp-json/wp/v2/posts?fields=id,title,content
- Just make sure you use valid field name.
I hope now you got basic idea of using WP REST API. You can learn more about it by reading API documentation at below link.
http://v2.wp-api.org/reference/
How to Make Android App for WordPress Blog
Below I have shared the example for making simple app. In this app I have fetched titles of some posts and displayed them in a list on one activity. When you will click on any title then it will open another activity and display the title and content of that particular post.
For fetching the data from internet I have used Volley library. If you don’t know how to use it then I would recommend you to read below tutorial.
Android Volley Tutorial to Get JSON from Server
Creating WordPress to Android App Project
- Open Android Studio and Create a new Project.
- We need the following dependencies to be added in our app level build.gradle file.
1 2 3 4 | compile 'com.google.code.gson:gson:2.2.+' compile 'com.android.volley:volley:1.0.0' |
- Add internet access permission by adding following line in AndroidManifest.xml file.
1 2 3 | <uses-permission android:name="android.permission.INTERNET" /> |
- First we will create a ListView in our activity_main.xml. In the ListView we will show all the latest post fetched from the REST API.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/postList"/> </LinearLayout> |
- Now create a new file named post.xml inside your layout directory and write the following code. This file will be used to show the selected post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/title" android:layout_marginBottom="5dp" android:textSize="20dp" android:padding="5dp" android:textColor="#000000"/> <WebView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/content" /> </LinearLayout> |
- Now come inside MainActivity.java and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | package com.wordpressappexample; import android.app.ProgressDialog; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import com.google.gson.Gson; import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity { String url = "http://www.thejavaprogrammer.com/wp-json/wp/v2/posts?filter[posts_per_page]=10&fields=id,title"; List<Object> list; Gson gson; ProgressDialog progressDialog; ListView postList; Map<String,Object> mapPost; Map<String,Object> mapTitle; int postID; String postTitle[]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); postList = (ListView)findViewById(R.id.postList); progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setMessage("Loading..."); progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.show(); StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String s) { gson = new Gson(); list = (List) gson.fromJson(s, List.class); postTitle = new String[list.size()]; for(int i=0;i<list.size();++i){ mapPost = (Map<String,Object>)list.get(i); mapTitle = (Map<String, Object>) mapPost.get("title"); postTitle[i] = (String) mapTitle.get("rendered"); } postList.setAdapter(new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,postTitle)); progressDialog.dismiss(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Toast.makeText(MainActivity.this, "Some error occurred", Toast.LENGTH_LONG).show(); } }); RequestQueue rQueue = Volley.newRequestQueue(MainActivity.this); rQueue.add(request); postList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mapPost = (Map<String,Object>)list.get(position); postID = ((Double)mapPost.get("id")).intValue(); Intent intent = new Intent(getApplicationContext(),Post.class); intent.putExtra("id", ""+postID); startActivity(intent); } }); } } |
- Now create a new Activity to display the selected post. I have created Post.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | package com.wordpressappexample; import android.app.ProgressDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebView; import android.widget.TextView; import android.widget.Toast; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import com.google.gson.Gson; import java.util.Map; public class Post extends AppCompatActivity { TextView title; WebView content; ProgressDialog progressDialog; Gson gson; Map<String, Object> mapPost; Map<String, Object> mapTitle; Map<String, Object> mapContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.post); final String id = getIntent().getExtras().getString("id"); title = (TextView) findViewById(R.id.title); content = (WebView)findViewById(R.id.content); progressDialog = new ProgressDialog(Post.this); progressDialog.setMessage("Loading..."); progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.show(); String url = "http://www.thejavaprogrammer.com/wp-json/wp/v2/posts/"+id+"?fields=title,content"; StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String s) { gson = new Gson(); mapPost = (Map<String, Object>) gson.fromJson(s, Map.class); mapTitle = (Map<String, Object>) mapPost.get("title"); mapContent = (Map<String, Object>) mapPost.get("content"); title.setText(mapTitle.get("rendered").toString()); content.loadData(mapContent.get("rendered").toString(),"text/html","UTF-8"); progressDialog.dismiss(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { progressDialog.dismiss(); Toast.makeText(Post.this, id, Toast.LENGTH_LONG).show(); } }); RequestQueue rQueue = Volley.newRequestQueue(Post.this); rQueue.add(request); } } |
- Now you can run your application. Just make sure that you have changed the URLs with your own blog and installed the REST API plugin correctly.

- Try clicking on one of the post and you will see the complete post.

- So our app is working fine. Yes we still need to work on improving the UI design, but this tutorial meant to explain you the concept. Now you can do whatever you want with your apps UI.
- You can also download the source code of this WordPress to Android App Tutorial from the link given below.
If you have any queries regarding this WordPress to Android App Tutorial, then ask them in comment section. I will try my best to help you.
Neeraj Mishra is a blogger, developer and freelancer from India. He has two blogs The Crazy Programmer and The Java Programmer where he writes about tutorial for various programming technologies like C, C++, Java and Android.
How to show images in a list view rows as well because only text is shown.
How to fetch it from WordPress blog and show into android list view.
To get image you must first set featured image for each post. Now when you fetch the json data for a post, it will contain a key with name “featured_media”. This key’s value contain the id for the media used in that post. Now using this id fetch data for media by URL “http://www.thejavaprogrammer.com/wp-json/wp/v2/media/77”, replace 77 at the end with media id. This will give a json data for media that contains urls of images used in that post with different sizes. Get url from that data and fetch image from that url. You can show this image in listview with title.
Hi Neeraj, I also have this problem of showing images in listview by getting the image from json output. I’ve researched other blogs but it is very complicated using arrays and adapters. I can’t modify your code using their technique because I’m just a newbie in programming. Can you make another tutorial using your code to put images on listview? Thanks!
hi, i have tried your code by there is problem , it shows “Some error occurred” and always loading spinner. what is the error ?
Hey, the same error I’m getting when i tried this code. Can you please let me know steps to solve this issue? Is there anything we have to configure in one of those plugin that we integrate in WordPress or it is something else?
Do let me know ASAP. Mail Id: developerPurvik@gmail.com
hi neeraj
but my featuredmedia tag showing like, ,”featured_media”:118 what to do ?
Not the link of featured media i am getting
Only no kam getting
Hi how can make it news in slider ?
kindly advise
works great except…. Umlauts! Don’t know why, but the german umlauts are being displayed correctly in the main activity (list of post titles). But in the post activity (post content) they are not displayed correctly. Totally unreadable if the text is german.
I have configured this as per shown here. But I;m getting Error every time when request calls. It also doesn’t display any error log when i tried with the vollyError().getMessage(). Please provide some info ASAP. Awaiting reply.
Can use it with wordpress.com sites?
Sir if content section contains images and text then how it will be separate??
Thank U so much for this tutorial!
You can have a tutorial about webview wordpress in app. because use API, Content display not correct.
Thank so much again!
Nice Tut! Thanks a lot! Next question: How to make prev and next list of posts? Thanks!
how to parse this according mapPost.get(“???”);
“wp:featuredmedia”: [
{
“embeddable”: true,
“href”: “http://zimmedarnews.com/wp-json/wp/v2/media/21560”
}
],
listview items 20, how to refrsh view add other posts?
Thanks for this Fantastic tutorial.
Please,
is there any other way of displaying the “content” aside from using the webview??
in such a scenario how do i deal with images embedded within the “content”??
I also need your assisance i’ve got a nested JSON Object Array as show below.
…
},
“terms”: {
“category”: [
{
“ID”: 2,
“name”: “Entertainment”,
“slug”: “entertainment”,
“description”: “And those who were seen dancing were thought to be insane by those who could not hear the music.”,
“taxonomy”: “category”,
“parent”: null,
“count”: 30,
“link”: “category/entertainment/”
}
]
How do i deal with this kind of case?
Hey can you please help me getting the tag names for each post from json results in android. I am able to get the title content and website etc, but getting the tags is not working.
OK good job
I would like to write a web service for my mobile application in Php so what hash type password should I use to connect with my wordpress website. Since WordPress does not use normal MD5 or Salt for encrypting passwords.
I want to set loadData to loadUrl in post. Java. Please tell how I can do that?
how to integrate firebase cloud messaging with wordpress so that when i create a new post i will also get push notification on my app.
I have also issue with customer create api of woocommerce in post method in android.Could you please help me. email: ravidhakad92@gmail.com
Thank you
Hello Belal ,
I am using WP REST API to getting data from android side, but data will take more time, i have use retrofit method for android side and data will come in json format.
How it is possible and what is the reason for this ?
1. It is possible for data will in large amount so the problem will accure ?
So please give me the suggestion for this ASAP….
How can we get mobile friendly html content as this does fit best on mobile application
Hi, I got the source code and compile it using android studio. when the app was launched it has an error massage: Unfortunately, WordPress App Example has stopped. Please help. Thanks.
You should check the log for the exact error
there is simpler way to achieve the same result
Hi! thanks I solved it already. The “+ id +” has spaces so I just deleted the spaces. I edit it to “+id+” and it worked! Thanks! My problem now is how to add categories. Do you have a post on how to make the categories on this app?
Hi Neeraj, I also have this problem of showing images in listview by getting the image from json output. I’ve researched other blogs but it is very complicated using arrays and adapters. I can’t modify your code using their technique because I’m just a newbie in programming. Can you make another tutorial using your code to put images on listview? Thanks!
please can you explain how to create new post with wordpress
API or how to create a new comment on a post using android volley?
We will try to post a new tutorial covering this thing soon
What if the wordpress website not owned by me, and no ability to install plugins by me, is there other way to fetch a wordpress posts / articles?
Thank you
Hello, in my post content there are Javascript functions, function that make a pop up when clicking a button, despite that i enabled Javascript for my WebView, in the logs i get an an error that the js function is not defined. How can i handle this?
Thanks.
thanks bro it was as simple as your blog title says simplifiedcoding
It works only for wordpress.org websites what should i do for wordpress.com website
Hey any idea how can I encode woocommerce orders and products
hello does anyone have the kindness to help you add images to the ListView? because it is impossible and we are many to have managed with voyre code to make the application but we can not add the images featured media to post
We will soon post an updated tutorial for this.
Its showing unchecked cast java.lang.object..for
mapPost = (Map)list.get(i);
mapTitle = (Map) mapPost.get(“title”);
postTitle[i] = (String) mapTitle.get(“rendered”);
}
Please help
in this tutorial thera are only 10 post acess.how can acess all post of our worpress blog