Welcome back in this video, we're going to set up retrofit to our applications so that we can then
actually load data from the news API.
And what we're going to add now has nothing to do with the news API, but it's generally something that
you need to do when you are working with retrofit and that is that you need to add the Gradle entry
to your implementations.
So here at the bottom in dependencies, you need to add your dependency for retrofit and the dependency
is going to go something like this.
It depends on the version that you're using.
So come square up, retrofit to retrofit.
In my case, the version is 2.0 9.0.
So if you want to be short used, the same one that I'm using here and this will allow us to use retrofit.
And then let's sink it really quickly.
And once it is sink, let's look at an article that we prepared just for the general understanding of
what's up.
So at the right hand side, when we were on this page, you will see a live example of the URL and let's
let's break it down, as I already talked about it a little bit, but let's see the basic URL that we
have because we need to understand this as we are going to use it in retrofit.
So we have the base URL, which is, for example, news API dot org slash v2 station.
And then comes the absolute path.
And you see GPS has to be part of the base URL as well.
So then we have the absolute path here, which, for example, would be the top headlines.
So if we want to have top headlines only then we would search for specifically top headlines and everything
that comes after the question mark.
Article three parameters such as the country where you then set it, for example, to the US.
Then if you have additional query parameters that you want to set, you put an eye and sign.
So this and sign, for example, is required in this case because we also always need to add the API
key.
So API key equals and then you would add your own API key, whatever that was.
So the response that we're getting is going to be in the form of JSON, and I talked about it just very
briefly.
But generally speaking, it's the JavaScript object notation that we are using.
So that's where the Sun comes from Java script, object notation.
So maybe it's easier if we if I put it like this, j.
So.
Hmm.
OK.
That is for the web.
Well, since Android doesn't understand this form, what we need to create, what it can understand,
which is a data class that will serve as a template for the type of data we are expecting from the API.
So let's understand this a little better.
So we have the curly brackets, right?
And everything that is inside of curly brackets denotes an object so an object can have an object inside
of it as well.
So you see, we have this bigger object.
This is that big JSON objects, which gives us the status, which gives us the total results, for example.
So this is a parameter which gives us, for example, the status which would be one member or one variable
of our object that says, OK, so it's a string, then we have the total results, which is going to
be a number.
So it's going to be an integer.
And then we have articles which will be, as you see here with the square brackets and array or a list.
So this is a list of objects once again.
So here we have another object, which then contains a source, which itself is an object which has
the property of ID and not a which can be null.
Then we have the name which would, for example, be a string in this case, YouTube.
Then we have the author, which, for example, would be null if we don't have any details about it.
Then we have the title.
So this is part of the next property rights of these.
Each time is a new property.
So so this is a property which is an object.
But then we have the next property, which is author, and the next one is this title and the description,
which is pretty long, which is a long string URL, which is a string the image to the URL, which is
a string when it was published, it is a string and then the content, which would also be string.
So we just need to bring this format now this JSON format, which is a very easily readable format for
us as humans but at the same time can be very easily extracted into objects that are understandable
for our program.
So that's really amazing and I love it, and that's why we're going to use it.
And you should love it as well, because it's going to allow you to build all kinds of applications.
OK, so what do we do with all of this?
Well, now we need to create models of it.
So we have this data class that is going to be called top news response where we get the status, which
is a string, but that it can be null.
So we're going to set it to null and it will be available.
Then we have the total results, which is an integer which will be null and then we have articles which
will be a list of top news article.
So now we need to define top news articles and we need to create another class which will create a well,
contain all of those top news article.
Contents, which itself has a sauce, which has to be an object and then has a bunch of properties.
So let's look at that.
We see we have a sauce, which will be a sauce class that we need to create.
Then we have the author, the title, the description and so forth.
And then the sauce, as I said, is the class itself because it has an ID and a name.
So it's because it's inside of these curly brackets.
The source basically says, OK, I am of type object, so let's go ahead and go over to our project
and create all of these data classes because we are going to require them.
So that's generally the approach that you take.
So you look at what is going to be the response that you're going to get.
And then you create the data classes or the models for your data that you're going to receive because
you need to get the JSON data converted into readable objects for your project.
And then you can use it like normal objects in Kotlin.
After we have added the retrofit independent dependency and we clicked on sync, now let's go ahead
and create all of those different data models that we need to create.
The first one being the top news response, so we need to create a new package here.
It's always good if you do that and you call it models, so all of our models are going to go in here.
We have a model already.
I think news data, you could say, is also a model, right?
So now let's go ahead and create a new Cortland class file and it will be a data class.
So let's call this one top news response.
OK, so this will contain, for example, as I stated earlier, the world contained a status.
The results and the top news articles.
OK, so this is what we need to set up, so we get this from our response, right?
So this is the top news response and then we get the status, the total result and then the actual articles
and the articles are going to be a list of something that we are going to call top news article.
Because what I want to have is only displayed the top news articles.
So I'm going to create a new class, create a separate file, which will be in the same destination.
And this will be a class I'm just going to call this one data class.
So this will be a top news article, and here I need to define what kind of parameters.
Top news article has, and I'm just going to paste them in here because otherwise, while you will just
look at me typing all the time, it's the same parameters that we get from here, right?
Source, author, title description and so forth.
And you can also get it from their documentation, right?
You don't have to read it in my article.
We we saw it earlier here, right where we had the articles, which at the source, the author, the
title description URL you're able to image, publish it and content.
So we need all of those in our project.
So that's what we have here.
But then the source, as you saw, was also an object.
So we need to create a class and here unfortunately, there is actually a Swiss class, so don't import
it, but instead create a new class called source.
OK, so I'm going to create and extract it to a separate class, which will be inside of models as well.
So this will also be a data class and the source data class is very simple.
It really just has an ID and a name.
So let's put this into brackets.
And it will be like this ID, which will be of type, string, syllable and name, which will be a type
string Nullarbor.
OK, so that's our models.
And now comes the interesting part.
So now we need to create something that is going to be our news service.
OK, so this will be an interface which will help us to send out the requests.
So let's go ahead and create a news service, which will be an interface.
I'm going to call this one news service.
All right, so what does this new service interface do for us?
Well, for now, it's just going to have to retrofit to HDTV get request.
So we need to get the data from the news API rights.
In order to get it, we need to create a get request and we need to pass in the value here of what we
want to get.
So, for example, I want to only get top headlines, so the top news, so to speak.
So then I need to create a function which will take care of that.
It's going to be called get top articles.
And here we need to pass in the query.
So our search requests, so to speak.
So here at Query, which is another retrofit to HTP annotation.
And then we need to pass in what we want to pass as to the value.
So for example, I want to search by a specific country.
So I'm going to get the country as a string.
Then I'm going to have another query.
So here, another pet query keyword, which will be the API key.
And this will be an API key of type string.
Like so.
And this will return a call to top news response.
So we need to import call.
Which is the one from an retrofit, too.
And we need to import.
Top news response, which is this one here?
So basically what we are saying is please get me the top articles where the country is, whatever we
are passing to you as the first parameter or is the country parameter.
And then the API key that we're passing to you, as well as a string and return a couple of top news
response.
So it will give us this piece of data.
So an object that will be of tip top news response, which will contain the the status, the total results
of the amount of results and all of the articles that are inside of the top news.
OK, and now it's good practice to have an API object.
So let's go ahead and actually, let's put all of this stuff into a network.
So we had models, we have the components, we have our UI and now let's go ahead and create the new
folder or new package, which I'm going to call network.
So here all of the network classes will go in.
So I'm going to direct news service because it is off type while it is a network class or interface.
So let's refactor it.
So now we have the new service in there, but I will also create an object in here.
Which will contain details about, for example, the base URL and then the retrofit builder and so forth.
So let's call this one API like so this will just be an object, as I said, and we need to have the
base URL, so we need to know which link we need to use.
So it's always common to build it like this.
You just create this constant, which will be a base URL when you're working with constants that's always
common to use all caps letters.
So this will be not changed.
HTTPS forward slash forward slash news API dot org slash v to slash.
OK, so this will be the base URL.
You can find it here as well.
So if you look at the base URL, you can see it here.
This is the base here.
All the way up to here is what is the base URL?
And then you could either search for everything or you could search for top headlines.
And we are specifically interested in top headlines, which is why we're using this thing also with
the country code.
So this was what we had said earlier in our get request.
So here it's a get request with the base URL up to here, then with additional parameters that you need
to pass, which we are going to do as well.
OK, so now let's get back to our project.
All right, so now let's initialize retrofit.
Therefore, I'm going to create a retrofit object, which I'm going to use the retrofit builder for
it.
So this is a method that that retrofit offers, and I'm going to Paris into base URL or set the base
URL better set with our base URL that we have set up earlier this one.
And then.
I built this like so built.
So this will be my retrofit objects or initializing the retrofit object here, and then I can use it.
So we'll now initialize the service class.
So therefore we set up the retrofit service, which will be of type news service, which is the interface
that we just created by lazily doing it.
That's just common practice to use by lazy here.
And then we create something with our retrofit object.
So here, retrofit, thought, create.
And here we create a new service class.
Java.
So that's what we need to pass to our create method.
So first, we create a variable for the base URL, then we set up the retrofit.
Then we add the base URL and build it, and then we initialize the service class using retrofit.
So that's what we're doing in this API object for now.
We will create a dedicated class to manage to request logic.
But we are going to do that in the next video because I think this video is already long enough.
So this was just a setup VIDEO.
You see, this is quite advanced content.
So it's not something where it can just build it in three lines of code and then show you something.
So we need to build up a lot of stuff and then everything comes together, OK?
So keep on following along and see you in the next video.