Welcome back.
Now it's time to actually make an API call, so instead of just making a toast here in our location
weather details, we actually want to connect to the API and get the data.
OK, so what we're going to use is retrofit.
So Volle retrofit and we prepared retrofit.
Right.
So retro fit and it will be of type retrofit.
OK, so this will be a retrofit object.
Now we need to import retrofit for this to work.
Let's do that.
And I need to import retrofit here.
The thing is, I will import retrofit altogether.
So let's go all the way up here.
And look at retrofit, so I want to use not just retrofit version two or whatever, I'm just going to
use all retrofit.
All right.
And then.
I will create a retro fit builder, so a lot of the stuff that I'm going to do and this and the next
few videos is going to be from the retrofit documentation.
So something doesn't make sense to you, then check out the documentation.
I will, of course, try to explain everything that I will build, but at the same time you can see
that you can always check out the documentation there.
You will find more details on everything and the code will be more in depth.
The explanations will be potentially also more in depth.
OK, I'm just going to use this third party library for the API calls.
So what we need to use is retrofit older that base you, Earl.
And here we can pass in the base here, which is the link.
And now, of course, at this point, it would make sense to have a constant so that we don't need to
pass on a string here, which will just be a magic string.
So let's go over to our constants and actually prepare decipherable here.
So let's prepare several variables, actually.
So first of all, we will need a constant, which will be our app ID, which will be of type string.
So this will be our API.
And in order to get that, you need to go to your and whether API you need to go to.
You're back and so Sinon API keys and then select an API key here.
OK, so you can give it a significant name, I'm just using random names here so you can copy this key
and then add that into your.
Constancia, so use your own.
Don't use mine because it won't work as well as your own key because it's limited to 60 calls per minute.
So if many users would use my key, then it would simply not work at one point.
So use your own key there.
You make sure that it will not be called as often, right.
And you will be able to test it correctly.
Then what we will need later on is also the basic URL.
So as I said, and here we need to pass in the basic URL.
So let's add a constant here as well.
So if we use it at multiple spots in our application, we then make a change to it.
We only need to make the change in this Constancia URL file here.
OK, so this base, your URL constant in our constants katee file.
OK, so the basic URL will also be of type string and it will be the link to the open weather map API.
So the base area will be API, that open weather map dot org slash data slash.
And then.
There are multiple different units.
OK, so for example, I want to use the metric unit, so I'm going to create this value here as well.
Metric unit, which will also be of type string and will be metric.
So, for example, we have Celsius or we have Fahrenheit and so forth.
So this is what this valve will take care of.
OK, at this point we can go back to our main activity and actually pass in a base URL here.
So I'm going to pass in the constants that base your out, which is going to be of course, this your
here.
So what I'm saying is I want to connect to this euro and then what we're going to add is a convertor,
so I'm going to add a converter factory.
So now the cool thing is I can pass in the Jaeson converter factory.
This is pretty powerful because this is just saying that it will transfer the data into the right format,
which is will be our Jaeson format.
So here we need to call create and finally we can build it.
OK, so.
How we do it, we say, OK, let's create a retrofit object, which we call retrofit, we build it based
on the base URL, then we will use the Jaeson format.
So to convert a factory here and then we build it.
So by default, if we don't add a header here, it will use UTF eight and it will encode Jason accordingly.
And at this point here, it will create a retrofit instance.
So basically what we're doing is what is explained here, so we create.
A retrofit builder, we used to base URL and then we build it.
The only thing that we also added was our Jizan converter factory.
As you can find further down, so there are different types of converters here.
You can see Jason Jackson, Mioshi and so forth, and I'm using the Jackson converter factory, so here
you can add the converter factory as well.
Now, the next thing that we need to add is the request method, so we will build an interface for this
where we get, for example, well, in our case, it's not the user's list, but in our case, it's
the weather.
So you can see here data up to this point.
This is the basic URL.
Well, up to this point is the base.
And then we have two point five, which is the latest version, and then we have the weather.
So this is.
What we need to add to the link and then you can see after that, we have our query, so this year after
the question mark is actually our query.
So up to that point, you could say that this is the basic URL, but it's not really because this part
could change.
Right.
So the base URL up to date data will always be the same.
The version could change and this quote usually shouldn't change.
But everything after the question mark here is our query.
So we create our own request based on values that we have, for example, our latitude or longitude
and our API key.
So ID is our API key.
And in order to use equerry in retrofit, what you can use is discovery keyword here, so you can see
you can add to Quarry, for example, sword, which could be a string, and what we're going to use
as doubles because latitude and longitude, they are doubles.
So in this case, they're just in four whole numbers, but usually latitude and longitude are a little
more precise.
You can see it here one of thirty nine point zero one or thirty five point zero two.
So we will need to use doubles there.
OK, so let's just prepare this interface that we will need in order to create the full link.
So basically, we want to have this whole link and adjust that accordingly and to our location, so
to our code to pretty much that we prepare.
So what I'm going to do is I'm going to create a new interface and therefore maybe it makes sense to
create a new folder here as well, which will be our network folder.
So I'm going to create a new package, which I'm going to call network.
And in there I'm going to create a new.
File and it will be an interface and I'm going to call this one Weather Service.
So in this interface, what we need.
First of all, this Atget, so we want to post a get request, so we need to pass that in and we need
to import get.
So let's do that here in port.
From retrofit.
And I'm going to use retrofit one, so retrofit HTP get.
And what do I want to get well, here in person, two point five weather, so as you recall, this was
part of the link and then I want to get the weather.
Where I have multiple Querrey objects, so here to query, which is at Querrey and here again, we need
to import retrofit Katzir, import retrofit HGP Querrey and the first one will be the latitude.
And what do I want to have the latitude.
Well, I double value.
OK, so for the latitude I want to have a double value, then I'm going to create another query which
will be the longitude.
So here it will be Laun double and then I need to app ID.
So here query or again at query here it's the app ID which will be the app ID, which in turn is a string
soheir string nullable.
And then there is one thing that we don't have yet, but we will also need later on.
So at Querrey, which will be units.
So if I want to pass the units as well for, for example, the system, so is it the metric system that
I want to use or what is it?
So here, for example, you can see you can add something called units, which will be metric.
So if you want to have it in Celsius, then you would only use metric.
If you want to have another system, then you can use another key word here.
So that's why I add that to the link as well.
And then just get weather function, should return something, it should return a call of weather response.
Now we need to import, call and weather response, so let's call.
From retrofit and let's import weather response from our own folder here.
So you can see now we use a call from retrofit, we use the HGP, get an extra Querrey and of course,
our own model, which is the weather response.
So basically, when we call this function, we want to run a call which will use our weather response
as the response of this whole call.
So basically we want to have a weather response object as a result, which will be a Jaeson object,
which will contain all of the different information, such as the coordinates, the weather list, the
base, the main divisibility and so forth.
So we will get all of this information and we get that from that call.
Now we can go ahead and use that service.
And our main activity, so where would be a good spot?
Well, here somewhere we created this retrofit object because now we can go ahead and create a service,
which will be our weather service.
So we create a service object which will use our weather service interface.
And here I'm going to say that this will be using our retrofit connection to we prepared and it's going
to create the weather service.
And here in brackets, you need to add the class, so here, Weather Service Class Jaba.
So what we do here is we met the service interface in which we declare the end point and the API type
in our case, it's a get API type and we do that along with the request parameters which are required.
So we want to connect to the base URL.
We want to use this weather service in order to make the URL complete.
We want as a response, a weather response, which is this object here.
And that's what we pretty much do with this code here.
So these six lines do this whole magic.
So those lines, they prepare the euro.
They say that what do we get as a result?
We want to use the virtual factory of Jaeson in order to bring it into the right format.
And then we start to service or we don't start to service.
But we prepared the service in order to finally then make a call with this service.
OK, so let's create a call, which will be call of weather response.
Where we use our service that we're prepared here and there, we call the get weather function, so
there's function here.
Now, of course, we need to pass and latitude longitude units and app ID.
So let's do that.
Let's say we want to use a latitude, longitude, then constants that metric unit, which is going to
be.
If you look at it, the units and then finally the app ID, so constants that have ID.
Now, of course, latitude and longitude are still missing.
So where do we get them from?
Well, it's best if we get them from whenever we call this function, so when we get the location weather
details, we want to have the latitude here.
So latitude, which will be a double and then longitude, which will also be a double.
Now, if we do that, of course, we need to change the call here, get location weather details, which
is in our home location callback, where we create a location callback, where we get the latitude from
the last location.
OK, and the same with the longitude.
So we need to pass those two over here.
So I'm just going to pass in the latitude and longitude when I call this location weather details,
which is dysfunctional we just created in which we check if we have an Internet connection.
If we do, then we use retrofit to establish an Internet connection with the service and then we start
to list call or we prepared this list called first, and then we can finally start this list call.
OK, so at this point we can go ahead and use this list call and Q. So you can see everything is connected
to each other.
So we need to first of all, prepare to retrofit and we can make a service or create a service based
on that retrofit.
And then will you make a list called based on that service that we prepared in which we need to pass
and all of this information as we have prepared in our get weather?
Function here in our weather service.
Interface, so now we can use NQ here.
Now, what is this?
Thank you.
Well, thank you is part of the call interface, which is or which comes with retrofit.
OK, so instead of retrofit, we have this interface called call, which has this function execute,
which is going to give us a response.
Then we have NQ, which requires a callback and then we have cancel and clone, OK, and we're going
to use NQ of this list called because this call is a call object, which is of course, this interface
that is delivered by retrofit.
And we need to pass a callback object here.
So in order to create a new cast callback object, I'm just going to use a call back whether response
object.
As the parameterized pass now here, if you want to use that, you can see it's complaining because
you need to implement members so you can need to implement on failure and on response.
So let's implement those two.
Now, it has those two because callback requires on response and on failure, so on response will be
once the successful HTP response is given and on failure will be called when we have an error.
So we need to implement something that should happen on response and on failure.
So what we will do is on response.
I will check if the response is success.
So was it successful, this response, and then I can create a weather list.
Variable, which will be the weather response, which will be in turn to respond, body, soul responds
that body.
So here.
If response is success, so here the nullifying it so you can see here once there is a successful response,
what I want to do is I want to get the body.
So what that body will be is basically this whole thing here.
So this whole string, so to speak, the whole Jason object, the biggest Chazen object, which contains
all of the sub object, so to speak.
OK, so I want to get this whole body which contains everything, and then I can potentially tear it
apart afterwards so I can break it into its little pieces.
But for now, I want to keep it simple and I want to just log it.
So let's lock it like I response result should be the weather list.
And, of course, this weather last year.
OK, so this is if the response is positive and the response is a success otherwise, so if the response
is not success, what I want to do is I want to create a variable here, which will be the response
code.
So we response code.
And I want to see what kind of response code I got.
So is it a positive response code while at this point it won't be because it's not success rate and
success is two hundred two or three hundred.
A response code of 200 to 300 is positive, but then four hundred, for example, is negative.
So I can say if it's 400, then log something to the error.
Logging and say around 400 bad connection or something like that, if the response code is 404, then
and it's something like not found a case of four or four, for example, then we can lock something
like error 404.
Not found, and otherwise, it's just going to be a generic error that we're going to throw.
So we're just going to say something like error or generic error.
OK, and then, of course, what shall we do when we get a failure response here?
Well, I'm just going to throw something like error and then here the throw will message.
We're just going to say arrow to string.
So whatever the role is, we're going to take the message and make a string out of it.
OK, so this was a very long video and we had to go through a lot of important stuff.
And I repeated myself sometimes because I think it's necessary to understand the whole connection of
it, because a lot of things that we have covered in this video are things that we have not seen before
and we have not used before.
So I tried to make the connection between the different things that we have learned and that we have
not learned and the documentation and also the classes that are in the background and everything.
So these are things that you learn by really digging deep into the documentation, by looking at some
example projects and so forth.
This is not something that you would just go down without having an experience, without having looked
at this API or without having looked at the third party library and stuff like that.
So you would really need to dig deep into the library in order to understand everything that is going
on.
OK, so I'm not expecting you to understand all of that directly and be able to replicate it directly.
But what I would like you to understand is that this is the procedure that you have to go through each
time that you want to use a third party library.
So you need to look up some examples.
Check it out, see how retrofit works.
But the beauty now is, you know, how retrofit works and now you will be able to use API calls, which
is basically your bread and butter when you work.
Has an Android developer.
All right, so at this point, we can test this application and we check our Lockette, then we can
see we get the response result here, which is a weather response object which has caught which is a
coordinate object, which contains the longitude and latitude.
Then we had the weather, which is a list, but we probably just get one value.
Yeah, we just get one value with an I.D. main description and I can pay station and so forth.
So we have everything here and that is beautiful.
All right.
So that works and.
Now we can go on and use this response in order to display the data accordingly.
OK, so I'd say let's go over to the next video.
I think this is one is long enough.
See you there.