Welcome back.
And this video, we are going to implement the functionality that allows us to access the Internet and
then also at one point, of course.
Ask the requests or sent the request in order to get the API call done and get all of the information
about the weather for a specific location.
So we have the location already.
Now we need to implement the Internet features.
So let's add those three permissions here.
Internet access networks, date and access Wi-Fi state, because those will allow us to see if we have
Internet and we can access websites.
All right.
The next thing that I want to do is to have a constant object.
So let's go ahead and create a new file here.
So new content and this one will be a file.
I'm going to call this one Constance.
All right.
So this one will be an object constant and it will contain all of our constants.
So the concept that I want to have for now is the content about whether we have Internet or not.
So is network available where I need to have the context and it will return a boolean.
So this one will help us to see if we have Internet or not or if we have a network or not.
And the thing is that in the past that would have been super easy.
All right.
So in the past we could have just used the connectivity manager in order to get that.
So let me show you how it worked up until API 29 or even 28 only.
But VUL connectivity manager.
So this one here is what we're going to need.
So I want to create a new object of it.
So context don't get system service.
And here for the system service, we need to pass.
The context that connectivity service as a connectivity manager object.
OK, so basically what's happening is that we have this line here where we create the connectivity manager
object, which will give us the connectivity service of our system, which we get in the context of
basically what we are in the main activity.
We want to check in that activity if we have Internet connections available or not.
So if we can connect to the Internet or not.
OK, and now in the past, as I said, would have been easy.
So let's just look at that first wall network info.
So I'm going to call it will be a connectivity manager, DOT Active Network info.
You can see this is deprecated, but that is just because it's the old fashioned way, so to speak.
But it still was super simple, right?
So we get the network info and then we can just return.
Network info is not null and network info is connected or connecting.
So that's the easy way.
That's how you can do it with the deprecated way.
But now in the latest version of Android studio and the Android API, what you need to do is to return
depending on the version.
So here if the built.
Version is SDK on the score int.
Greater equal built dot version Cotes.
Tara.
OK, so let me import bills here and then what is M.
M.
S Version 23?
So what I'm saying here is, is the SDK that is used Android 23 or younger.
If that's the case, then use some other code here, so then use the if code and otherwise use this
code that we prepared here.
OK, so we're going to use the deprecated approach for the old versions.
So for the old Android studio versions, meaning Android and before and for the newer ones, we're going
to use the following code.
So here we need to create a network which will be the connectivity manager, dot activity or active
network and not network info this time.
And then if that is empty, return false.
So if we can't get access to the network manager or the active network, then we will return.
False.
OK, so this whole is network available will return false.
But if we don't have an empty network, then what we want to do is we want to hear that the active network.
So here at the network, which will be connectivity manager, not get network.
Capabilities.
With that network.
So first we get the network, the active network, and then we check for the network capabilities.
OK, and if they are empty as well, then return false.
So when do we return true?
Well, let's look at it.
So here what we can check is return when so this is like an if statement, more or less so return when
the active network.
This one here has transport.
Network capabilities, Dot.
And here we can use the different ones, so wi fi, either net and so forth.
So if we have transport Wi-Fi.
Then we want to return, true.
OK, here, return true, so we already had return here, so we're saying return when this is the case.
True then when something else is the case.
So, for example, the same thing up to here, but this time we look at the transport cellular.
Return true.
And then finally, the transport either.
So maybe somebody uses land in order to connect his phone.
There are actually ways to do that and then finally else return false.
OK, so what we're doing here is we are checking for the version, if it's a.
New iPhone or a newer version on the phone, we get to network first, so the active network, if it
doesn't exist, we will return false.
Then we used this active network to check its capabilities.
And if it doesn't exist, so if it's null, then we return false.
Here as well, then we return true in the case that we have wi fi, that we have cellular Internet connection
or that we have either net, and if we have none of those three, we will also return false.
This is basically for the newer versions of Android and for the very old versions, we are going to
use the old approach, which is to use active network info and then return is connected or connecting.
So this will return a boolean here and you can see it's deprecated.
So it's all Java code, so to speak, that is deprecated, which doesn't mean that it's not going to
work.
Right.
Just means that.
It's not recommended, and when you want to know more about it, you can, of course, check out the
stack overflow.
You can see here connectivity manager, Active Network is deprecated, also an API level 29.
So there are alternative ways to do it.
And that's what we've just done.
So this one here, OK, so where we get to transport Wi-Fi, transport cellular and transport Ethernet,
I just wanted to show you the different options that you have here.
All right.
So at this point, we will be able to know whether we have Internet or not.
Now, the next thing that I want to implement is another.
Rachel.
Implementation here, which means another third party library, and in this case, we are going to use
retrofit.
OK, so let's implement that and sync to file and look at retrofit.
So retrofit is a type safe Klein for Android and Java, so if you want to know more about retrofit,
then check out this website here.
You can see squarer GitHub trial slash retrofit and this is going to be the API to it.
So it's not super long, but basically what it allows you to do is to use requests a lot quicker and
a lot easier than what we have done so far.
So it allows you to very quickly get an API call out and then get the result very quickly.
So you can see retrofit class generates an implementation of the GitHub service interface.
So here we can use and retrofit object with the builder and we can just add the base, your URL and
build and then run that service in order to finally then call the HTTP request so you can make a synchronous
or an asynchronous HTTP request to the remote web server, which in our case will be the open weather
map API.
So the link that we have here, if we use any of those links, for example, the one with the latitude
and longitude.
So this is going to be the link, even though it will not be the sample, but it will be another link.
OK, all right.
Now that it is sinked, we can use retrofit.
So let's go ahead and go over to our main activity, because that's where the magic happens.
And let's create a function which will get us to weather, so private fun, get location, weather details.
So I'm going to call it.
And what we need to check is if we have an Internet connection so we can go to Constance, Dot, his
network available.
And we need to put this into context, which is this so in this main activity, do we have an Internet
connection?
And if we do, then want to write a little toast for now.
So here we will try and implement a code that will, of course, need to be executed once we have an
Internet connection.
We want to get the weather details, but for now, it's just going to be this little toast.
And otherwise we're going to write a little other toast, which is just going to say no Internet connection.
All right.
So no Internet connection available.
Now, what would be a good spot to get the location weather details?
Well, and our location callback, which we use to get the location of the user once we have the longitude
and latitude here, we should call this get location weather details, because we're going to use variables
for that later on that we can then either pass to this function or we can have global variables which
take care of that.
So either way, we now have prepared to code.
Now, of course, for now, we can just test this and see if we have Internet and our on our phone.
So you can see I'm on flight mode now.
So it should display that I don't have Internet.
So let's see, no Internet connection available now let's deactivate flight mode, let's turn on Wi-Fi
and the other data.
So just cellular data, let's run this again.
And you have connected to the Internet now you can make an API call.
All right.
So now everything is prepared.
And in the next video, we're going to see how to make a call to our API.
So see you in the next video.