Retrofit basics – Introduction

Reading Time: 3 minutes

In the introduction part of the Retrofit basics tutorial we are going to summarize the following chapters and give you an overview what you will learn, if you do this tutorial.

In this tutorial we are going to show you how you can send (register user), post (fetch all users) and get your profile from the database of a WordPress website, what we have prepared already for you.

For all of this, we are going to use the Retrofit library, which is a type-safe HTTP client for Android. To get the data from the website, we will use a WordPress REST API, called Swagger.

REST

Frist of all, we have to clarify what does REST mean. It stands for Representational State Transfer and it a software architectural style. It defines a set of constraints to be used for creating Web services. Web services that conform to the REST architectural style, called RESTful Web services, provide interoperability between computer systems on the Internet. RESTful Web services allow the requesting systems to access and manipulate textual representations of Web resources by using a uniform and predefined set of stateless operations. Source: WikiPedia

API

Next, what about we need talk a bit, is API, which stands for Application Programming Interface. It enables two applications to cumminicate with each other, share data between each other.

Source: Understanding REST

Eg. if you have an Instagram account, then you can get the likes, shares and some data about your followers also.

WordPress REST API

The WordPress REST API allows us to send, update and delete data using our application on our Android device. So, we can directly communicate with the database of our WordPress website. It works with some restrictions, but in our tutorial the most important part is to show you how does Retrofit work.

Retrofit

Retrofit is a REST Client for Android created by Square. With the help of it we can quite easily retrieve and upload JSON (or other structured data) using a REST based webservice.

We can configure in Retrofit the converter, which is used for the data serialization. In this tutorial we are going to use Gson, but eg Moshi is also a great choice.

Retrofit uses the OkHttp library for HTTP requests. OkHTTP is an open source project designed to be an efficient HTTP client.

The Sample app

MVVM

In the sample application we will use many features of Android. We are going to build up the app using the MVVM design pattern. It means, we will have a repository, which will connect directly to the Retrofit client. Using this repository, we can call its methods from a ViewModel, which handles the data.

After the creation of the Retrofit client, the repository and the ViewModel, we can observe the LiveDatas from the ViewModel inside of the user interfaces, which are in our case Fragments. It means, when the data is changing, then the UI will get automatically the changed data.

Navigation Component

For the navigation, we will use the Navigation Component library. You can find for it a basic tutorial on our website: Navigation Component basics

Data Binding

To simplify the call of the view from the layout and ensure the null safety usage, we will use Data Binding also. For this, we have also a basic tutorial. 😊 Data Binding basics

GitHub

And one more thing. The sample will be available on GitHub. In case if you arr stucked, then you can check the needed branch, and you can continue learning 😊. Of course, for this we have made also a tutorial: GitHub basics

Questions

I hope the description was understandable and clear. But, if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂

 


Follow and like us:
Click to rate this post!
[Total: 0 Average: 0]

Leave a comment

stay informed!

Subscribe to receive exclusive content and notifications