DEV Community

loading...
Cover image for Understanding Marvels API from an Android Developer Perspective.

Understanding Marvels API from an Android Developer Perspective.

Kelvin Pere
Android Developer.
・4 min read

An interesting aspect of working as an Android Developer is working with some type of remote data by connecting to the internet. And often times we would have to rely on some type of API to use in our apps.

I have look at some API Documentation and wow, its been nothing but fun. In fact I think that would be another topic for a separate article. In this article, we are going to look at the Marvels api and see how we can understand some of it in using them in your android apps.

Problem

Honestly I would say the api docs of Marvel can do much better in helping those who build client side applications to effectively use it. I feel you shouldn't bother going outside the docs in learning how to use it.

Getting your API KEY

This is pretty straightforward - you simply go to this url https://developer.marvel.com and then create a free account which allows you to make 3000 calls per day on the api.

Parameters Needed

We would be focusing on using the API to fetch a list of public comics with some meta-data.

This url carefully explains what parameters would be needed in using the API https://developer.marvel.com/documentation/authorization. Below are the parameters needed:

  • Public Key =="apikey" (Note "apikey" != "apiKey" when passing them as parameters to your @GET function while using Retrofit)
  • ts (Also know as a timestamp, it just wants to know the exact time the request was made, I don't know why but that's what it needs).
  • hash : A md5 digest of your timestamp,public key and private key which are provided in your dashboard. If you are curious about how it really works you could do more research about that if you want, but its not really important for this aspect. Basically its just an Algorithm that converts some type of data in this instance your ts + public key and private key into a length string value. In code this would be a function that concatenates all of the strings and creates the hash value which should be in a form of string.

Now you would say, well thats pretty obvious and straight to the point. Well it is, but wasn't stated like that for those that might want to use it in a mobile client.

Looking at the docs Picture of client auth you might want to follow this instructions for your android app which is client. Please follow the instructions for server side Applications because the client in focus is for browser based applications.

Testing it in PostMan
If you are like me and always test your API Endpoints using Post Man then you should take a look at this amazing GitHub repository https://github.com/doamaral/postmanmarvelapi

Having fun with the API

To really see how the api works and know what type of JSON data would be returned you should go to the Interactive Documentation tab section

Looking at the GET public apis Public API You can decide to play around with it and know how the JSON Response is and then use that to model your Kotlin or Java Objects.

Android Project

Now we know how the API Works we can then use them in our application as we would when consuming other API. I would briefly drop some hints below that can guide you.

Kotlin Model

Looking at the JSON Response after playing around with it, you would see that it involves we having 4 data class in to represent our models. Model Image

Comic.kt Comic Model

ComicResponse.kt ComicResponse Model

Data.kt Data Model

Thumbnail.ktThumbnail Model

Assuming you decide using Retrofit, then you would need to create an interface and declare your method/function without any body. Remember that you are making a @GET request so it has to use the above mentioned query parameters using the @Query Annotations like this: Retrofit Interface

So looking at my Constant.kt fileConstant

Then you can go ahead and use whatever method that seems convenient for you to use in fetching the data to your activity.

Note
While using Glide to load your images from the API please remember it requires the thumbnail path of type string and also the extension type appended to it. Adapter

I hope this short article will help you use the Marvel API with ease because it offers some really cool responses that you can use in your Android Apps. Till next time bye :)

Discussion (0)