MVP + Retrofit2 — API Call Made Easier
For the last week I studied about Retrofit2 for handling HTTP request, as in the near future I need to add some API call in the project. After studying (and trying) it, I found that it’s actually quite easy to use! Note that since MeetU used MVP (Model-View-Presenter) architecture, I’m going to use that architecture in my explanation below :)
Example: Fetching message from Firebase
Let’s say we want to get message object from Firebase that is structured like this:
First thing first, to add Retrofit2 in our project, we need to add these dependencies in our app’s build.gradle
:
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.google.code.gson:gson:2.8.2'
In order to access the internet, don’t forget to add this line in the AndroidManifest.xml
file:
<uses-permission android:name="android.permission.INTERNET"/>
Now, let’s move on to the app. First, let’s make our API classes:
Basically, I made a base class for API-related class and extend it. Let’s focus on the Service in MessageApi
, spesifically the getMessage
method:
- It is a
GET
request - It will Call request which will be responded by
MessageResponse
class - The path for the request will be
findUrl()/{user}.json
, where{user}
will be replaced byuser
from the parameter. For example, ifuser = "ayaze"
, then the path will befindUrl()/ayaze.json
.
Next, let’s move on to the model. Basically, I want to have a model for the message and for the response containing it. According to the Firebase’s structure, the message is stored with key some_message
. Okay, let’s create the appropriate model for message first:
By default, Retrofit2 will use the variable name as the key when it tried to serialize or deserialize JSON format. For the message model above, that means someMessage
will be stored with key someMessage
.
So now we realize we have a trouble with the name: We named itsomeMessage,
but, in Firebase I stored it assome_message
. Now, how do we tackle this?
@SerializedName
come to the rescue! Basically, @SerializedName
let us to set the key name. Now, someMessage
will be serialized to some_message
. Now we won’t have trouble with variable name anymore.
Let’s move on to the response class:
retrieveData
is used to call MessageApi
's GetMessage
method asynchronously. When retrieveData
is called, it will enqueue the callback which will be fired after we get the message. Okay, now how do we use this method? That’s where presenter kicks in!
Here, the presenter will have a listener which will act during each retrieveMessage
call. So, all that is left is to create the listener. Who will be the listener? Yes, it is none other than the view!
As the view will represent the listener, it will have to implement both onSuccess()
and onError()
method. Also, the view need to have the presenter to be called. So,
- View will have presenter, and
- Presenter will have view as listener
It can be a bit confusing, but I found it pretty funny.
Here’s the stub for the view:
It’s been a long journey, and we are almost there! Now, all that is left is to try it. And here is what I got when I tried it in my phone:
Yay, we successfully got the Firebase data!
Conclusion
Overall, I found that implementing Retrofit2 in MVP architecture is quite easy and fun. Sure, several trial-and-error was needed until it works. But, knowing how to use it sure will be handy in the future.