๐Ÿ‘๐Ÿ‘ Retrofit review ๐Ÿ‘๐Ÿ‘

Or how to use Kotlin + RxJava to get some cats

Costa F
Costa F
Feb 18 ยท 4 min read

This is a spin-off of the โ€œAndroid RxJava in 5 minutesโ€ article I wrote a while back and has stuff that you might find useful for this one.

You can find it here:


What is an API anyway?

It seems like everyone and their mother is running an API on the internets. If you are not too sure what an API is, or how to consume (????) it no less, fear not. This tutorial will teach you how you too can copy-paste your way to a lead dev position in Silicon Valley.

Cats are important

Doing something of value is paramount to finding happiness aside from browsing stale memes on r/programmerhumor.

So, of course, we will concentrate on the most important aspect of all, cats (or dogs if you are into that sort of thing).

Retrofit might as well be a synonym for boring and the tutorials out there are really trying hard to be the solution to chronic insomnia so letโ€™s just write something man.

Source code can be found here:

What you will need

Just click File -> New project in Android Studio 3 and include Kotlin support, AndroidX artifacts and an empty activity pre-made. Press next on everything. Jetbrains really understands its market (monkeys banging the keyboard like me) and basically writes everything for you these days.

Let the thing finish building.

Dependencies

Go to the build.gradle (Module: app) file in the dependencies block. It should have these lines in it at least:

The cat whisperer

Thereโ€™s loads of public APIs out there to test out.

We gonna go for this one here I found randomly browsing:

Request an API key and have a look at the docs. Or you can just follow along after your get your key.

Setting things up like you know what you are doing

Get yourself a generic repository class like this one below.

Reading the docs and going here https://api.thecatapi.com/v1/images/search we get some JSON back.

Put that in the http://www.jsonschema2pojo.org/.

Select:

Source Type : JSON

Annotation style : Gson

Hit preview and you will see an Example class created. This will give you some guidance on the data class you will need so you can convert the response from the server to something of meaning to you.

Here it is anyway:

Need a file to put the GET request in too.

And the final piece of the puzzle tying these together.


I lied.

Retro cat

Get your MainActivity and set up a button so you can test this request yourself.

Since RxJava is being used you gotta be lifecycle aware, hence the compositeDisposableOnPause variable. For more info check the โ€œAndroid RxJava in 5 minutesโ€ linked at the top.

The activity:

Someone might say that all this is really verbose and long-winded.

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

Plus, this is a 5 minute tutorial, what did you expect?

Wew lad

Before you run this make sure to replace the apiKey variable with your own, personal, key.

Give it a run and tap the button. There should be a toast popping up telling you what happened (good or bad). The Logcat will have more information too!

For now, itโ€™s a bunch of links, which donโ€™t seem all that great. Using Glide or Picasso will take care of that problem.

Follow on to the next part where we try to introduce the ViewModel and take the logic out of the MainActivity and into the ViewModel.

Later.


Smash like and subscribe guys new videos every Wednesday

Costa F

Written by

Costa F

https://www.markedasduplicate.com/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and weโ€™ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium โ€” and support writers while youโ€™re at it. Just $5/month. Upgrade