Although this is a beginner article,you should already know how to create a simple app on android studio.
What you will learn
- MVVM architecture
- Sending API requests using Retrofit
- Testing APIs using PostMan
I will be explaining MVVM architecture using a project I worked on,check it on android-sms.
What is MVVM architecture?
When I first came across I was confused why we would need an architecture yet our apps can work without using them,well let me give some context using a story.
A driver talking to a passenger
A person designated to collect bus fare
In the first picture, driver does all the work:driving,dropping passengers,collecting bus fares,looking for passengers e.t.c and this can be tiresome while on the second picture we have someone to do all the work while driver's job is just to drive and wait for signals on where to stop and not worrying himself on who has paid and who hasn't.
Take the bus as your app and the driver acts as an activity,although all the work can be done by the activity it can be chaotic.On the second app,the driver is still the activity but this time we have introduced another component to deal with the data while the driver(activity) does less work and this can bring a good experience.
MVVM is an software architectural pattern focused on separation of concerns i.e each component does work independently. Hope you got the idea from the example above. It consists of three parts:
Pic courtesy of MindOrks
- Model - this is where the data comes from,either from local storage or remote data source
- View - the view is what the user interacts with (activity ) and its work is to display data and respond to users interactions. It observes the data from the viewModel.
- ViewModel - its role is to hold data which will be observed by the view. Unlike the activity, it is lifecycle aware - this means that it can live longer than the activity hence the data won't be lost when there's an interuption on the view.
Read more on activity's and viewmodel's lifecycles
Testing APIs using postman
This project was focused on sending requests via API,so before doing so I felt it is good to learn how to test the API first before making a request.
While working with APIs,we have different HTTP methods;
- GET - used to get data.
- POST - used to send data to server and creates or updates a resource.
- DELETE - well,this is used to delete data on the server.
When making a request it is good to understand the API - method,parameters and expected response.Go to Postman and create an account,you can download or use the web version.
- Click on workspaces, new workspace,enter name and create workspace.
- Click on the + sign to create new request
I am using MoveSms so you can also create an account or use any other API.
Select the HTTP method,enter the API url, enter the fields described on the API documentation and their corresponding data(I removed username and phone number to take the pic).
When you click send you should get a response,every request must have a response to show the status of the request - either successful or failed.
That's it for this part, on next part I'll discuss about dependancy injection and the project mentioned at the beginning.
Thanks for reading,let's connect on twitter.