‫Hello, my friends, and welcome back.
‫In the previous videos we created the data classes and we created the API service interface.
‫Now we are going to create the retrofit instance.
‫Since we are using the retrofit for network calls, let's create a class that provides us an instance
‫of a retrofit and should be a singleton instance.
‫For that I will go to here to this package.
‫I will create a new Kotlin class and I will name it as retrofit instance.
‫I should or I prefer.
‫It's not an obligatory thing, but I prefer including the keywords in creating the classes in order
‫to understand later on that this class is a retrofit instance or this class is an album service or a
‫modal class or something like that.
‫Let's me create it.
‫And here this class retrofit instance, it will include a companion object because it's a singleton
‫and I've lectured about the companion object before.
‫So.
‫This is the companion object.
‫I want to create a singleton instance of retrofit.
‫I want this application to use only one retrofit instance without creating more and more retrofit instances
‫every time I need to fetch the data in order to prevent memory leaks, save time, save network calls,
‫and so on.
‫So I will create a base URL here.
‫Function sorry variable equal to.
‫As I told you, this is the end point and this is the base URL.
‫Include the base URL inside the retrofit instance and include the endpoints into the service APIs.
‫It's very simple, guys.
‫I hope you understand them well.
‫So I will include this json placeholder dot type code.com.
‫Into this string.
‫Now I will create a function to return for me this retrofit instance.
‫So function, get retrofit function or instance.
‫Sorry.
‫It's returning type as.
‫Of type retrofit.
‫And I need to see here or to check for returning and building this retrofit.
‫So return retrofit dot builder.
‫I'm calling the builder method or to build.
‫And before I build here, I will go and customize this retrofit by providing it with the base URL.
‫Base url here telling to fetch the data.
‫I'm telling here the retrofit instance to fetch the data from the base URL and add the converter use
‫the converter factory which is the Json.
‫Converter Json converter factory.
‫Here I am telling.
‫A retrofit to use the Json converter factory dot create.
‫And here.
‫Jason Builder Dot.
‫Create.
‫And the last thing I need to add is the build method.
‫What I've done here, I'm creating an instance of retrofit.
‫I am providing it with the base URL.
‫I'm telling it, Hey, go to this link, the Json placeholder dot type code.com and start fetching the
‫data.
‫But which data?
‫I'm providing it in the album service with the endpoint.
‫So ad for this base URL, this endpoint and start fetching the data from it.
‫Add converter factory and I'm just linking the Json with Json and start building this retrofit instance.
‫So congratulations guys.
‫We created the three steps of retrofit implementation.
‫Now let's start getting the response and display them into the main activity and the UI of our application.