Create two REST API code classes for easily accessing the Pixabay website and returning a list of photos to use for the app. Learn how Retrofit does the heavy lifting.
- Retrofit is a library from Square that makes it easy to call REST API's on the web. In this video, we will create the two classes needed to get photos from a website. We will be using the Pixabay site to provide an API for retrieving photos that we can use in our sample app. To start using Retrofit, you will need to create an interface to the REST API we will be using. In the project window, let's create a package name API, right click on your current folder and select new, package and give the name API.
Press okay. Now right click on the API folder, select new Kotlin File/class, and give the name Photo API. Press tab, and type I to change the type to interface. Press okay. Retrofit uses interfaces for creating the REST service, inside the interface, add the following, fun get photos, call photo, list, hit tab, select option enter, to import the Retrofit 2call.
Notice that generics are the same in Kotlin as in Java, as shown by the call photo list return type. You will still use brackets to surround the generics class to be used. This function will return the type of call photo list, call is Retrofit type, when you receive this type back, you will have to either have to enqueue it, which means make an asynchronous call, or execute it, which will run it synchronously. Now we'll add an annotation that Retrofit needs to create the API.
Above the function, add the following annotation, at get, let's hit tab to import our Retrofit, get annotation, type quote, question mark key equals, now we need to go to the Pixabay docs page. This is where you will get your unique Pixabay API key. I'll copy the key. Now, let's paste that key in. Add q=nature for a query, another parameter of image type=photo, that says it will be getting photo images.
This annotation gives the key for accessing the Pixabay API, and requests images with the keyword of nature, now right click on API and choose new, Kotlin file/class, and give the name, photo retriever. Press tab, type c for class, and press okay. Next, add the following variable, private val service, photo API, notice the underline here, which states that we have not initialized it, we'll do that later.
Next, let's create an initialization method for creating the service. Since primary constructors cannot contain any code, Kotlin uses the init code block for construction code. Type init, so tab to complete, now let's create the Retrofit object. Retrofit.Builder, put this on another line, base URL, then we're going to add the Pixabay site for that, Pixabay.com slash API forward slash.
This'll create a Retrofit instance with the base URL needed to access Pixabay, note the base URL that is pointing to Pixabay. We'll add our Gson converter factory to convert JSON data to our data class. So we'll add converter factory, and we'll add the Gson converter factory, we'll create that, next, we will build. This will create our Retrofit object. Now, let's create our service.
Dot create, and we'll use our photo API class, and we'll use class dot Java. Class dot Java is Kotlin's way of referencing a Java class object. What this does is use Retrofit's builder class to construct an API service for us. We need to define the base URL that we will use as well as the factory class that will convert the results. Now we can use the service variable to make API calls.
Now let's create a method to get those photos. Fun, get photos, and we'll be passing in a callback, I've typed callback, photo list. Now let's create a variable, we'll just give it a name of call, we'll use a service to get our photos, and that call will not do anything until we actually in queue it, which will run it asynchronously. And we'll pass in the callback variable.
So this method takes a callback that will be called once the API call has finished. This method calls our service and then enqueues it, which means that it will run asynchronously. We have just created two files, an interface that defines our REST API, and a class that will create that service and make the API call for us. All the user of this service has to do is create a callback object.
- Setting up Kotlin
- Converting activities from Java to Kotlin
- Creating an emulator
- Installing and adding Android libraries
- Reviewing basic Kotlin syntax
- Using variables
- Creating functions
- Retrieving images using the REST API from Pixabay
- Layering image views using a FrameLayout and a RelativeLayout
- Building the RecyclerView adapter
- Displaying a high-resolution photo of the image in the RecyclerView