Retrofit review ????????Or how to use Kotlin + RxJava to get some catsCosta FBlockedUnblockFollowFollowingFeb 17This 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:Android RxJava in 5 minutesThese go(o)gles do noffing!medium.
comWhat 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 importantDoing 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:CostaFot/android–retro-electroContribute to CostaFot/android–retro-electro development by creating an account on GitHub.
comWhat you will needJust 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.
DependenciesGo to the build.
gradle (Module: app) file in the dependencies block.
It should have these lines in it at least:The cat whispererThere’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.
comRequest 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 doingGet yourself a generic repository class like this one below.
Reading the docs and going here https://api.
com/v1/images/search we get some JSON back.
Put that in the http://www.
org/ , select Source Type : JSON and 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.
Retro catGet 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 ladBefore 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 Picasso or Glide will take care of that problem and will be part of this series (if you are anxious to try it out go to https://github.
Stay tuned for the next part where we’ll try to introduce the ViewModel and take the logic out of the MainActivity and into the ViewModel.
Smash like and subscribe guys new videos every Wednesday.