Welcome back.
In this video, you are going to learn how to get data from a website by using an HTP request, which
will be done asynchronously, and then used that data, which will be in the JSON format in order to
display the data in your application.
So we're just going to use it to display in the Lockette, not directly in the UI, but it's really
just not that much of a hassle if you understand what's going on in this video.
You can do that easily.
And of course, we're going to see that in projects later on, especially when we build our weather
app.
And then, of course, in our firebase example, because Firebase also uses this same approach, though
they also use chasten in order to display the data or store the data correctly.
All right.
So let's go ahead and look at what we have here.
So, first of all, we need those two permissions here, access networks, data and Internet.
Then we need to have this line here uses clear text traffic.
This allows us to access websites such as this Moqui website because otherwise this just will not work.
So we will not be able to access the website and get the data from there.
Then I have this dialogue, custom progress, which is just this progress bar with a text view.
So that's basically this thing that you can see here.
We have used that quite a bit already and then connectivity.
May I just have this text file which says Hello World, because we're not going to really do anything
with the TextView because we're going to log the data and then let's go to our main activity, because
here what we need is an inner class, which will be our async class.
So I'm going to create a new private in class.
I'm going to call this one call API login async task.
All right, and this will be an async task, so any void and string is what we need to add here for
this async task.
And then, of course, you need to import async task and implement its members.
So for now, we're just going to implement do in background.
And then I will create a little object here, which will allow me to create a custom dialog so private
in it or late.
And it's here var custom progress dialogue, which will be a dialogue.
OK, so.
What I want to do is I want to add those two methods in here so you can, of course, copy them from
the project itself as well.
So what we have is our show, Progress Dialogue and Kensal Progress Dialogue.
So two methods.
This one will just show this layout that we prepared, this one here and the other one will cancel its
out, will dismiss it.
OK, because what I want to do is I want to display this dialogue as soon as the download starts.
So pretty much on pre execute I want to display it.
So here on pre execute you can see we have this super on pre executed so that will be executed.
This code just before whatever is in doing background will be started.
So just before we connect to the Internet.
OK, so here I'm just going to show progress.
Dialogue.
All right, and then there is another one which is on post execute.
So once the execution of the thing that we want to run in the background, which is in our case, connecting
to the Internet.
There I want to finish this whole progress dialogue so here can slow progress dialogue.
And here, I will also, of course, work with the data that is the result strength so you can see we
get a result.
So before we get that result, let's do something in the background.
OK, so what we need to do in the background is to return a strength.
And in order to do that, we, of course, first of all, need to have a string.
So I'm going to create this var result, which will be of type string and then I need to establish a
connection.
So I'm going to establish a connection which will be of type htp your URL connection.
All right, this one will be a nullable and I'm going to sign it because I want to establish the connection
later on.
I don't want to do it directly here so something could go wrong.
That's why I create this try and catch block, because each time you try to establish a connection,
for example, the user could not have Internet or we could try to access the Internet even though we
don't have the permission and so forth.
All right.
So in here, I'm just going to use a new URL, which is going to be a U.
RL object, and I need to import that.
And here, by the way, we're going to use the.
Jova want.
And I'm going to use this URL that you can see here so you can create your very own your URL, you saw
how to do that in the last video.
Just use Moqui here, create one or create a link here that you can then use and then use that link
in your application.
So here you need to pass it in.
It needs to be a straight zero and whatever you pass on to the euro and then you can establish a connection.
So we can say connection should be your open connection as HTP Euro connection.
OK, so what we had here, so we can make sure that it really will be an HTP oil connection and now
we can use that connection.
All right, so this connection, you can define some properties here, for example, do input and do
output.
So in our case, do input.
I'm going to set that to true and do output as well.
So do input is to get data or do output is to be sent data.
So I'm just going to set both to true.
And this is only necessary if you really want to also send data because by default to input will be
true and do output will be false.
OK, so in this case, I'm just going to set both of them to true.
Just to show you both of those properties.
All right, before I show you how to send data, let's look at how we can receive data so Voll HTP result
is going to be of type integer and it will be the connection DOT response code.
So you get a response code, which is very similar to what we have seen here in Moqui.
You can see here this status code.
You get a response code which contains the status code as well.
All right, so.
We can now check if the status quo is two hundred, so how do we do that?
Well, we do HTP result is htp your Eurorail connection, dot htp underscore.
OK, so what is this htp underscore.
OK.
Well it's the static final int 200.
So it's the status code two hundred which means OK, so we have a connection and it worked out OK.
You can see here and a royal connection.
You have all of these static final integers.
So that's basically constant.
All right.
So what we're saying here is, is the HTP result.
Two hundred, it's the same thing, but maybe some time someone will decide to change the numbers and
then it would not work anymore.
So in this case, it's good to use the correct approach, which is to get it from the HPL connection
class.
All right, so now that we have established a connection, we want to read to data from the website
that we have and in order to do well in this case from this website here.
So in order to do so, what we need is an input stream and it will be based on the connection, that
input stream.
So we get it from that connection again.
Now we can create a reader.
So reader will be a buffet reader and it needs to and of course, need to import that again.
So that's important.
Real quick and then input stream reader needs to be passed here and here.
We need to pass our input stream.
All right, so this is our reader, and now we can use the reader in order to read every single line.
But the thing is, we, first of all need to have a string builder which makes string out of whatever
we get as data there.
So we'll string builder will be our string builder object, which will allow us to build strings and
then we can go ahead and go through every single line.
But therefore, I'm just going to create a line variable, which will be a string nullable.
So thing is, this can go wrong again.
So we have another try here.
And what we say is while the reader still has something to read.
So Reline.
And also.
The line is equal to it.
Is not equal now
then we can apply that to the string builder, so Stringbean builder, append the current line plus.
Hey, Lambrick.
So now the strike and, of course, go wrong, so let's have a catch here with an Iowa exception.
So input output exceptional.
Because we're using input streams and so forth, right, and then here I'm just going to say print stack,
trace.
And finally, what I will try to do is to close the input stream.
So here I'm going to try again because here something can go wrong again when closing and opening up.
Everything can go wrong here.
And we catch that with another IO exception.
I'll accept.
All right, and again, I'm just going to print the status here.
So this one here.
So now we had something called resolve here, right?
So doing background is going to return a result, which is going to be a strength.
So at this point, we get all of this data from the lines and we have the string BOLO, which happens
to data all the time.
And now we can go ahead and get that string builder and return that to our result.
So here we can go ahead and say results.
Should be our string builder.
To history.
And here to strengthen.
So it's going to make a string out of whatever it has in itself, as you can see, it upended the lines,
so it went through the website line by line.
In our case, it went through it with this line, then the next point and the next line until there
were no lines anymore.
So basically just loaded this whole set of strings, so basically this is just a text, right?
This whole thing, even though we say Jason and everything to it, but it's just a string.
It's just one text.
So the result here will be the whole text, just as one string.
And by the way, all of that was in this statement, so if the oil connection was OK.
So what will happen if it was not OK?
Well, what I'm just going to say is the result should be Conexion response message.
So whatever we got as a response message, for example, bad request or not able to connect to whatever
the result will be, we will get that as a result and we will then later on display it.
OK, so here we still have this other tri blog that we had.
So here this is to try and we get an error here because it's expecting the catch block here as well.
So let's catch the errors that potentially could arise.
And one of them is the socket timeout exception.
So we try to connect it to it.
But for some reason, the Internet connection wasn't good enough or something like that.
So in this case, I'm just going to set the result to Conexion time out and then in another case, which
is the general exception.
So just an exception here.
I'm just going to say, all right, the result should be something like error and then whatever the
error was.
So plus e dot message.
So whatever the error message then was here.
And finally, I will disconnect the connection.
So disconnect.
If there is a connection.
Then disconnect, and if there is none, then just don't do anything, that's what we do here with this
question mark.
And then finally concede, still complaining here because this whole.
Doing background requires a string to be returned.
So now let's return that string.
Let's go ahead and return our results.
Which is the strength that we pretty much filled up with all the relevant information now, at this
point, we have gone through everything that we needed in order to get to data from the website in terms
of a string.
So now we just have this string, which is the result of our doing background and then in our own post
execute, we get the result back as a string again.
In this case, it's a nullable.
But what we can do now with this is to use the result in order to display.
So let's log it.
Look, I and I'm going to just say JSON response result will be the result and import log here.
So let's do that real quick and board a log.
OK, at this point, we can already test it, so this will happen straight when we start our application,
it will start us.
And by the way, we cannot test it.
It would work by itself.
But of course, we need to create an object of our call API async task here because otherwise it won't
do anything.
So in our uncreate in the main activity, let's create an object of our call API, a log in async task
and of course, execute.
So we need to execute this task here.
All right, now let's run it again.
And for a very quick moment, barely seeable, it was there.
So now let's go to the Lochhead and let's search for Jason.
And what we will see is that we have received this response here.
So you can see Jason responds, result.
And here it starts.
And then that's the whole result.
You have successfully logged in user ID and so forth.
So now you know how you can connect to the Internet and download something from a page.
And that is just the first step to seeing everything with the Jason formate.
So that's what we're going to do in the next video.
We're going to break it down into Jason objects with the old fashioned way.
And then later on we will see how we can use Jason in order to make the whole thing that we're going
to do in the next video a lot easier.
But I think it's still important to understand the basics of it before we go into using third party
libraries, because who knows how long they're going to work or if they're going to be changed or deprecated
or whatever.
It's always good to know the general approach and understand that.
All right.
So see you in the next video.