Android App for your WordPress Site with REST API

Should you build a mobile app for your WordPress site or should you go with a responsive theme, this is a discussion that has split my WordPress developer friends.

A bunch of them stick with the notion of using a responsive theme design and design the site so it shows as a mobile app on a mobile device and shows as a web app in a web browser.

If you take a look at my website, if you open it on mobile it looks different then when you browse it on a laptop. This is all done with responsive design using HTML, CSS that should be sufficient for a basic WordPress blog.

Try my website on your mobile device and you find a different view than on a laptop. Here is how it looks like on a mobile device.

Mobile APP

Back to the other side of my WordPress developer friends, they say you should build an app for mobile devices, their argument is that with your own app you are shown as an image in the list of apps on your device, and that would give you more exposure and likelihood to get more followers.

So, I decided as an experiment, to build an Android app. I started out with defining the business requirements for the app, very basic – show categories, the latest posts and show the full posts from my WordPress site.

Next, I did a wireframe on how I wanted the mobile app to look like and then I started to develop the android app using Android Studio with Gradle build.

To get the categories, posts and detailed post data from my WordPress site, I used the WordPress REST API – to work with the REST API in Android I choose to use the Android Volley library.

Here is a good article – Android working with Volley Library if you are interested to learn how to use the Volley Library.

Setting things up

The first thing you need to do is install the REST API plugin on your WordPress site. After the REST API plugin is installed and activated, you can visit your site http://yoursite.com/wp-json/wp/v2/ to see the basic JSON object. It looks something like this:

{"namespace":"wp\/v2","routes":{"\/wp\/v2":{"namespace":"wp\/v2","methods":["GET"],"endpoints":[{"methods":["GET"],"args":{"namespace":{"required":false,"default":"wp\/v2"}

You can see elements of your site by appending the URL, such as http://yoursite.com/wp-json/wp/v2/posts/, or http://yoursite.com/wp-json/wp/v2/pages/.

That’s the data that we’ll use to pull into the mobile app.

The mobile app

This shows how my mobile app looks like.

Custom post types (CPT)

The Android app is simply going to display custom post types from WordPress. The only thing we are getting from the website is the data through the REST API.

To get custom post types, we can access

http://yoursite.com/wp-json/wp/v2/posts/types

I created a post type called “News” for this app, so I can access the data I need at

/wp-json/wp/v2/posts?type=news

My JSON data for the news CPT looks something like this:

 {
"id": 5836
"date": "2015-12-29T14:41:26"
"date_gmt": "2015-12-29T13:41:26"
"guid": {
"rendered": "https://torbjornzetterlund.com/?page_id=5836"
}-
"modified": "2015-12-29T14:41:26"
"modified_gmt": "2015-12-29T13:41:26"
"slug": "browse"
"type": "page"
"link": "https://torbjornzetterlund.com/browse/"
"title": {
"rendered": "Browse"
}...

If you don’t have a custom post type, that’s ok, you can access your posts at http://yoursite.com/wp-json/wp/v2/posts. Go ahead and visit that URL to view your JSON data.

That object gives us access to everything we need: title, excerpt, content, featured image, author, etc. We can now pull that data into our app, and display it any way we want.

Just compile your app with Android study and publish it on Google Play and you have your own Mobile App working with your WordPress site.

If you are interested in getting your own mobile app, just get in contact and I can help you with all aspects of developing a mobile app strategy for you.


Posted

in

, , ,

by

Comments

16 responses to “Android App for your WordPress Site with REST API”

  1. maraya Avatar
    maraya

    can you send me this sourcode?

    1. torbjornzetterlund Avatar

      I do not give out my source code, I put to much time and money in it, I can sell you a copy of the source code for Euro 35.

      1. ron Avatar
        ron

        I might be interested in purchasing the source code from you. Have a paypal?

        1. torbjornzetterlund Avatar

          I accept paypal, go to this url https://torbjornzetterlund.com/purchase/
          and select the package that you would like to buy.

          Payment is handled by PayPal.

          Torbjorn

  2. Jafid Avatar
    Jafid

    Hi, I’m trying to create a native android app and I’m using the WP Rest API and the Android Volley library, currently im receiving in the response of the Volley the JSON from my custom post type, but I dont know whats the next step to display the post correctly in the app, can you help me getting that to work?

  3. torbjornzetterlund Avatar

    I don;t know your web platform, you should look for designs that incorporate responsive in there theme design. If you using worpress – then you can look for a theme at Codecanyon

  4. jaswanth Avatar
    jaswanth

    Hey as of Dec.2016, as Rest API is built into WP 4.7, do we still need to install a plugin ?

    1. torbjornzetterlund Avatar

      That is correct, you will not need the WP Rest API, there are some changes to the Android App for WP 4.7 – I will update the article.

  5. Jethendra Avatar
    Jethendra

    Hi , I have a doubt.. Can the content contain image Urls along with text ? If yes how can we show text and Image seperately.. Please help me out.. Thanks in advance

    1. torbjornzetterlund Avatar

      Jethendra,

      What do you mean with “contain image Urls along with text”, do you want to display an image or do you want to display a link to an image?

      Torbjorn

  6. Ali Avatar
    Ali

    Hello Sir, i got 2 questions, how can i get all the css for the WordPress site at once, instead of using the browser Inspect.
    Second questio is, which is better, Volley or Retrofit
    Thanx

    1. torbjornzetterlund Avatar

      I prefer Volley, it’s a personal choice, what you are used to, there is a bit of a learning curve using a new library.

      You should find the .css library of a theme, in the WP Folder wp-content->themes->

      The android app I have do not use the the .css of the the theme of my wordpress site, the Android app is a separate UI design.

      If you want to have your WordPress site to display in a mobile browser, you need a responsive WordPress theme that smoothly adjusts its layout based on the screen size and resolution. That is not an Android app, you would need to use a browser app on your phone to get the responsive WordPress theme to show.

      I hope that answers your questions.

  7. Ali Avatar
    Ali

    Sir,
    Does you code handle JavaScript function within the content for the Posts?

    1. torbjornzetterlund Avatar

      No, it does not execute the javascript inside a post.

  8. manasvi sawant Avatar
    manasvi sawant

    hi I wanted to ask you that I am trying to link up my wordpress with android and I am trying to fetching details of all the users but I am not getting all the users using rest api is there any anything more changes i need to do?

Leave a Reply

Your email address will not be published. Required fields are marked *