Alright, so we've now got our data downloading, and the app can parse out the information we want from the raw json data.
So in this video, we're going to build up the url to allow different tags to be specified.
Now we're going to be adding a search facility to the app later, so that users can display photos matching
different criteria. Now for that to work,
we need to provide the tags they want as a parameter in our url.
Alright so let's see how that works, so we know what we're trying to achieve here.
So I'm going to use Firefox for this because it's easier to see the result without having to scan through
the raw Json code. OK I'll paste in our link.
Now I just said that we wouldn't have to scan for the Json, yet
we've got a load of raw Json to read now. Now that's fine in our app,
and the reason we're getting Json is because of the last two parameters in the url, these last two
bits here, the app format equals Json and at no Json callback equals one.
Now when we build up out url in the app we're going to need these two parameters, but I'm going to delete
them from the url in the address bar.
So basically delete everything from the ampersand format right through to the end.
Once we do that we actually get a format that Firefox can display with images.
Aright so our base url is
what you can see up here, I'll just size that up, minus the tags.
So the tags equal after the question mark is the first parameter, and that's the one that we're going
to be changing when users search for different photos.
Now at the moment as you can see the tags parameter's set to Android comma Oreo,
so changing that will return different photos.
So let's have a look for some sunsets by replacing this android oreo with the word sunset.
Now the photos change all the time but we've now got some photos of sunsets. Alright so in our code,
we're going to be building up the url so that we can provide different values for this tag parameter.
Alright so let's go back to the code to get that working. Now before get raw data's created and it's execute
method called,
we build up the url so that it's got the correct parameters,
and we're going to do that in the Create uri method that we'll write next. Now when we use our create uri
function, the code's going to look similar to what I'm about to type in MainActivity.
So we're going into MainActivity, and we're going to go into the onCreate method, and we're going to change the
code a little bit.
So we're going to, above the val getRawData line, we're going to put Val url is equal to create uri
parentheses double quotes,
and I'm going to copy this link, right up to but not including the question mark.
So basically the dot gne is where it ends. I'm going to paste that in as the first argument to our create uri.
And then I'm going to put another parameter on the end of that, so comma double quotes android
comma oreo, another parameter comma double quotes en-us,
and lastly comma true. And then what we're going to do is change this getRawData dot execute with the parameter.
We're going to change that to instead, pass the url. So I can see that we've actually added that now,
and obviously we've got an error at the moment because we haven't written the create uri function.
I'm going to create that next after the onCreate method, so just immediately below
that. So private fun create uri parentheses, and it's going to be baseURL colon string.
We're just matching the parameters that we've defined or we're showing on line 20.
So the next one is search criteria, search criteria, and that's going to be a string also, languages the
next one, so lang colon string.
And the last one's going to be matchAll colon Boolean, then outside of the parentheses, the right parentheses we're going to have
colon String because we're returning a string.
And we'll open a code block there, and obviously that fixes the error now on line 20.
So let's go ahead and actually write that function though. I'm going to start with Log.d parentheses tag comma double
quotes dot createUri starts. Then we're going to do var
uri is equal to capital U r i dot parse baseURL, that's parsed to this function. And we'll do var builder
equals uri dot buildUpon. I'm just going to scroll this up a little bit. Alright next we're going to do builder equals builder dot
appendQueryParameter and its parentheses and double quotes tags, being the name of the parameter, comma
searchCriteria.
The next line, we're going to do builder equals builder.appendQueryParameter again.
This time it's going to be double quotes, tagmode being the name of the parameter, comma, then I'm going to type
if, parentheses matchAll double quotes ALL in uppercase, else, ANY in
uppercase. And next we're going to do builder equals builder dot appendQueryParameter parentheses double
quotes, and it's going to be lang this time, for language, and the argument will be lang, builder equals builder
dot appendQueryParameter, format this time, double quotes comma
and it's json in double quotes, in lower case there, and builder equals builder dot appendQueryParameter. Next one's going to be
double quotes, so it's nojsoncallback, which is the next parameter, comma one in double quotes.
Then we're going to type uri equals builder dot build,
then we're going to return uri dot toString. Alright so what are we doing here.
Well we start off parsing the base url on line 30, and that's to create a uri object, which I've called
uri with a lowercase u.
So you then need to build the parameters on top of that. Now the code uses the uri dot builder
object to get a builder using the buildUpon command. Obviously that's on line 31. Now
next we're using the builders appendQueryParameter function to add each parameter to the uri.
Now the appendQueryParameter function takes care of separating the parameters with a question
mark or ampersand as appropriate,
and also makes sure that the next step results in a valid uri.
So each time we're appending another parameter. We get a builder object back from the calls to append
query parameter, with the new parameter added to the end of its previous value. Now because each step
returns a builder,
we don't need to assign the result to a variable. We just call the next function directly on the return
value of the previous function call. So what we can actually do here is just to show, to demonstrate that this is working,
we can actually delete this last line here,
this builder equals builder, and go right back to there, and delete. And that's basically doing the equivalent thing,
and then we can actually go back and build up and do all of this on the one line. So we can come back to here and do
that, and we can come back to here, and
do that, and for the last one, like so. Now that line starts getting pretty long and scrolling off screen,
so it's a good idea to split it and start each call on a new line. So we can do something like buildUpon,
we can even come back onto here, and do the same there.
and we haven't used a variable at all
as you can see. So again that's avoiding the use of a variable,
and again just to make it a little bit tidier, we can separate these on a different line. So we can put
dot, and then do each append on a separate line just to make it a little bit easier to read, like I'm doing now.
And in actual fact we don't even need the builder variable at all if we do that. We can chain the code
to dot build upon as well. So we can just use the dot build upon,
so we can just highlight this and delete, just to make it a little bit tidier. We can go, we can add the dot here,
dot build, like so. So basically as you can see now I've removed the reference to the variable, where we've just got
our uri that's been built up as calling the relevant functions and then returning the two strings,
so the string version of the built up uri, ready for us to actually send to flickr. And I'm showing you
this for a reason, and the reason is that a lot of Java classes are designed in this way.
So the function append query parameter, in this case, returns the object it was called on,
so you can actually keep chaining function calls together.
So it should now be pretty obvious from this code that we're just appending a load of parameters, one after the other.
Now append query parameter takes two arguments. The first one's a string, and
that's the name of the parameter that we wanted to add to the url.
The second one's the value that we want to specify for the parameter, and that should probably be pretty
straight forward now
that you can see that, because you've used to, and have seen tags, tagmode and format and no json
callback, when we were calling those links, the urls directly in a browser.
And basically there's no need to store the value in uri
when we're only going to return it, and this function is defined to return a string.
So we finish the chain by calling two strings, so we can even do that by getting rid of the variable all
together, and just putting return and adding dot toString
on the end, and returning the redundant variable.
So again there's no need to store the value in uri when we're only going to return it, and this function
is defined to return a string,
so I've just chained to, or added to the chain by calling dot toString and returned that instead
of uri dot toString. And again you'll find that sort of thing done a lot in Java classes.
It results in pretty compact code and the classes have been designed to use that way.
So although it might look a bit confusing at first, but it's just calling a function on the return value of
the previous function call, and so on down the chain.
So now that we've done that, let's just run this application again just to make sure that it's still
working, given we've actually made some changes to it. We'll open our log cat
and you can see we've got our dot createUri function call,
so that's working nicely, and we can still see that we've got our photo objects being created, and our onDataAvailable
called with the array list of photos.
So everything looks good and is working nicely, Alright so I'm going to stop the video here now.
In the next one we'll make a start on the user interface, so that we can display the data that we're now downloading.
So see you in the next video.