{"id":898,"date":"2019-12-28T07:30:52","date_gmt":"2019-12-27T21:30:52","guid":{"rendered":"https:\/\/learntodroid.com\/?p=898"},"modified":"2021-01-13T09:46:24","modified_gmt":"2021-01-12T23:46:24","slug":"consuming-a-rest-api-using-retrofit2-with-the-mvvm-pattern-in-android","status":"publish","type":"post","link":"http:\/\/10.0.0.14:32769\/consuming-a-rest-api-using-retrofit2-with-the-mvvm-pattern-in-android\/","title":{"rendered":"Consuming a REST API using Retrofit2 with the MVVM Pattern in Android"},"content":{"rendered":"\n
Design patterns are important to help us create cleaner more extensible code with a clear separation of concerns. Understanding how all the Android Jetpack architecture components fit together in the Model View ViewModel (MVVM) design pattern is not a simple task and adding integrating with an API to the mix can increase the learning curve. After doing some research on how to use Retrofit2 to consume an API while following the MVVM pattern, I have put together this tutorial to share what I have learnt.<\/p>\n\n\n\n
To consume a REST API using Retrofit2 using the MVVM pattern in Android the you will need to complete the following steps.<\/p>\n\n\n\n
In the article I will take you through a tutorial of consuming the REST API offered by Google for Google Books using Retrofit2 inside an Android app architected using the Model View ViewModel design pattern in Java.<\/p>\n\n\n\n
All of the code samples that will be shown in this article in the tutorial section are available publicly on GitHub in the Learn To Droid<\/a> MVVM REST API repository<\/a>. <\/p>\n\n\n\n Before we jump into the tutorial I will also cover the following.<\/p>\n\n\n\n Retrofit<\/a> is a HTTP client for Android and Java by Square, Inc that simplifies the implementation of integrating with a REST API both for generating requests and handling responses.<\/p>\n\n\n\n Retrofit is easy to use for developers as it automates a lot of the work required to integrate with an API by transforming an Java interface that you write containing annotations and method declarations of the operations on the API that you consume.<\/p>\n\n\n\n Later in the post in the tutorial section I will show an example of how you can use Retrofit2 in our repository to integration with an API.<\/p>\n\n\n\n MVVM stands for Model View ViewModel and it is a design pattern which enables you to create a separation of concerns from the different components within your app in addition to allowing you to drive the user interface from your model.<\/p>\n\n\n\n In MVVM, the Model is made up of the data models, business logic and application data.<\/p>\n\n\n\n In MVVM, the View is the structure, layout and appearance of what a user will see in your app. <\/p>\n\n\n\n In MVVM, the ViewModel is used by the View to transform data in the model to display in the View.<\/p>\n\n\n\n Under the banner of Android Jetpack various Architecture Components<\/a> have been released by the Android development team in part to enable the use of Model View ViewModel design pattern in Android.<\/p>\n\n\n\n Examples of Jetpack Architecture Components used in the MVVM design pattern include:<\/p>\n\n\n\n LiveData is an observable data holder that is lifecycle aware. Inside your View you can add an observer to the LiveData so that when the LiveData is updated, your View (for example a Activity or Fragment) is notified so that you can update your user interface with the new data. The ViewModel is generally used to update the LiveData, however LiveData can also be modified in the repository on certain events such as handling an API response.<\/p>\n\n\n\n To learn more about LiveData and the benefits for using it, check out the video below from the Android Developers YouTube channel<\/a>.<\/p>\n\n\n\nWhat is Retrofit?<\/h2>\n\n\n\n
What is MVVM?<\/h2>\n\n\n\n
How can the MVVM pattern be applied in Android app development<\/h2>\n\n\n\n