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:
Contribute to CostaFot/android--retro-electro development by creating an account on GitHub.
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.
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:
TheCatAPI - Cats as a Service, Everyday is Caturday.
A public service API all about Cats, free to use when making your fancy new App, Website or Service.
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/.
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.
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.
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?
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.
👏👏 Glide review 👏👏
Or how to load images from the internets when you don’t know what you are doing
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.
Retrofit review : The sequel
Or how to use MVVM to get some cats so your code is not all over the place
Smash like and subscribe guys new videos every Wednesday