Android Kotlin: Consume Wordpress API via RSS Feed
If you’ve consumed a WordPress API then I’m sure you’re aware of the RESTful API wordpress exposes to you. This normally looks like
GET yourwebsiteurl.com/wp-json/wp/v2/posts
This is pretty neat and handy especially if you want to fetch data from your WordPress site or any other WordPress based site. Having worked with wordpress myself, I’ve found myself in interesting situations where I wanted to consume the wordpress API and get the latest posts. However, I was having trouble getting the data from the API as the site/wordpress admin may have disabled the REST API to prevent unauthorized access of their data.
For this reason, I decided to write a simple Kotlin application that would consume the wordpress API and get the latest posts.
While doing my research on how best I would fetch data from a wordpress site that had disabled the default REST API, I found a solution that was simple and effective.
Using the site’s rss feed. This is very ingenious as it does not necessarily limit consuming of data from Wordpress based sites. As long as the said site has RSS feed enabled then jackpot.
A way you can find out if a site has RSS feed enabled is by visiting the site and right clicking to view page source. Once you’ve done this you can search for RSS feed in the source code.
For this article I am going to consume https://nairobinow.wordpress.com to get the latest posts. to confirm if a wordpress website has rss feed enabled simply add /feed at the end of the URL. This should load the RSS feed for the said site which is in xml format. Voila! we have free data we can access and present to our app’s frontend. In order to consume the data we need to parse the XML file by converting it to a JSON response.
Getting Started
To get started we need to setup a few things. First, we need to import the libraries that will allow us to process and present the data. The Build Gradle should look something like this: build.gradle
We use Retrofit to consume the json response and Jsoup to parse html content in the json response. In this article the assumption is that you know how to setup a new android project. In the case you don’t know how to setup a new android project then you can refer to the following article.(https://medium.com/mobile-app-development-publication/setup-kotlin-for-android-studio-1bffdf1362e8)
After setting up a new project we import our libraries above by placing them in the app’s build.gradle file.
Create RecyclerView + Adapter
Since we will be populating the recycler view with the data we get from the API we need to create a recycler view and an adapter to populate the recycler view. Follow this article to learn how to create a recycler view and an adapter. [Android RecyclerView Tutorial](https://www.raywenderlich.com/1560485-android-recyclerview-tutorial-with-kotlin)
Setup Repository and API interface
Our repository will be responsible for fetching the data from the API. We will pass the repository to the view model to fetch the data.
Our Our API interface will be respinsible for our API calls. As a hack we need to convert our xml response to a json response. To do this we use https://www.toptal.com which allows us to pass the wordpress site’s url to the toptal api and get the json response. You do this by calling
https://www.toptal.com/developers/feed2json/convert?url=<pass the wordpress site url as a parameter> in our case we will make a call to https://www.toptal.com/developers/feed2json/convert?url=https://nairobinow.wordpress.com/feed/
This returns a json response that we can in turn consume on our app.
Observe LiveData from ViewModel before populating the recycler view
In order to populate the recycler view, we need to observe the LiveData from the view model. On Response Success, we will pass this data to our adapter and pass the adapter to the recycler view.
And just like that we have been able to fetch data from a wordpress site without using the conventional Wordpress API. Best part is that you can consume data from any website that exposes their RSS feed.
You can find the project source here: Download