https://torbjornzetterlund.com/wp-content/uploads/2015/11/Android_Robot_200-960x600_c.png

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 web browser.

If you take a look at  my website, if you open it in a 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 me 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 intrested 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.

You can get my Android App from here to see how it works

en_generic_rgb_wo_45

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

[contact-form][contact-field label=’Name’ type=’name’ required=’1’/][contact-field label=’Email’ type=’email’ required=’1’/][contact-field label=’Website’ type=’url’/][contact-field label=’Comment’ type=’textarea’ required=’1’/][/contact-form]

11 Responses

  • maraya on June 14, 2016, 18:56:50

    can you send me this sourcode?

    Reply to maraya
  • Jafid on June 22, 2016, 17:45:08

    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?

    Reply to Jafid
  • Nell on September 14, 2016, 20:51:50

    Howdy! Quick question that’s emtirely off topic. Do you know how to make your
    site mobile friendly? My weblog looks weird when viewing fropm my iphone 4.
    I’m trying to find a theme or plugin that might be able to correct this problem.
    If yyou have any recommendations, please share. With thanks!

    Reply to Nell
  • jaswanth on December 23, 2016, 06:32:07

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

    Reply to jaswanth
  • Jethendra on February 15, 2017, 08:42:31

    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

    Reply to Jethendra
    • torbjornzetterlund on February 15, 2017, 15:07:10

      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

      Reply to torbjornzetterlund

Menu