ANDROID SERIES 2: USING RETROFIT TO CONSUME APIs
A practical demonstration of how to use the GET annotation in retrofit to receive data.
Prologue
These are the articles in these series prior to this:
- Android Series: A Practical Guide To Understanding Retrofit.
- Android Series: Using Retrofit To Consume APIs.
In the previous article, consuming API using the @Post annotation was discussed. I also wrote about the importance and significance of these annotations.
In this article, we will be using the @Get annotation. We will be making use of coinmarketcap API to show the first 50 coins and their properties. I will make use of two activities. The first activity shows the number of coins while the second activity shows the detail for each coins. The source code to this project can be found here.
https://api.coinmarketcap.com/
Please note in case the API isn’t available in future : This API is now deprecated and will be taken offline soon. Please switch to the new CoinMarketCap API to avoid interruptions in service. (https://pro.coinmarketcap.com/migrate/)
A Little Diversion
The @Query annotation and the @Path annotation are often used as annotated parameters in a service method.
The difference between the @Query annotation in retrofit and the @Path annotation is quite salient. The @Path specifies the path for a particular item in a URL. On the other hand, we make use of the @Query annotation whenever we want to filter, sort or perform some kind of categorical analysis on the API.
Case Study
Let us consider coinmarketcap API:
https://api.coinmarketcap.com/v1/ticker/
This API displays the default number of coins on coinmarketcap and the coin properties.
Whenever we use the @Query annotation on a particular API, to let’s say obtain the first 50 coins on coinmarketcap.com, our final result is this:
https://api.coinmarketcap.com/v1/ticker/?limit=50
Or depending on the API documentation, we choose to show the properties of just 2 coins (Bitcoin and Doge), our final result will look like this:
https://api.coinmarketcap.com/v1/ticker/filter?coin_one=Bitcoin&coin_two=Doge
Note that 50 here is an integer.
Considering the @Path annotation…
Also, still considering the coinmarketcap API. Let’s imagine there are lots of incredible functionalities that can be done with the API. Let’s imagine I want to buy a coin for a user from coinmarketcap (obviously not real because coinmarketcap doesn’t sell coins 😄), I have to specify the ID of the user.
https://api.coinmarketcap.com/v1/ticker/user/6
6 is the ID of the user I want to buy a coin for.
It’s possible we conclude that question mark is always generated with the @Query annotation but not with the @Path annotation.
Enough Talk! Show me your CODES…
Step 1. Add Necessary Dependency to Your app build.gradle.
CardView, RecyclerView and Retrofit Libraries are Needed in this Project.
Step 2: Spice up your activity_main 😋
activity_main 👇
Step 3: Create an item_layout for your recyclerview
Step 4: Examine the API to be consumed (taking note of its key and value)
https://api.coinmarketcap.com/v1/ticker/?limit=50
Looking at this, you will notice that the array does not have a key.
What do I mean by this array doesn’t have a key? Take a look at the snippet below.
The key in this case is data. Unlike the former, it is very easy to associate the value with this key. Unfortunately coinmarketcap API doesn’t provide a key for the array so we have to make do with it like that.
Step 5: Next, create your MODEL class,
Make use of the @Serialized annotation as discussed in previous tutorials. After creating your model class, you implement the Parcelable interface, and the methods are generated for you.
Note: If you have not used Parcelable interface previously, don’t be scared — Estefania Cassingena Navone article on implementing Parcelable explains the concept of Parcelable and why it’s been implemented in this article.
Step 6: Set up your recyclerView adapter
Note: If you do not understand my CoinAdapter, check out my previous article on writing a mature recyclerview.
Step 7: Time for Network set-up 1! — Setting up Retrofit Client
Step 8: Time for Network set-up 2! — Setting up Interface for method call.
Note: If you do not understand Step 7 and 8, please refer back to the previous articles. Detailed explanations can be found there.