1 00:00:04,250 --> 00:00:06,629 Alright, so we've now got some data 2 00:00:06,629 --> 00:00:08,460 downloading, so it's time to do 3 00:00:08,460 --> 00:00:09,360 something with it. 4 00:00:09,360 --> 00:00:11,190 So let's start by having another look at 5 00:00:11,190 --> 00:00:13,830 the XML in the browser. Now I'm going to 6 00:00:13,830 --> 00:00:16,470 use Chrome, because we can view the XML 7 00:00:16,470 --> 00:00:18,000 in a much clearer way than some other 8 00:00:18,000 --> 00:00:20,939 browsers. Chrome's inability to render 9 00:00:20,939 --> 00:00:23,250 the RSS feed is great when you want to 10 00:00:23,250 --> 00:00:26,609 view the feeds XML. So I'm going to open 11 00:00:26,609 --> 00:00:30,510 a browser, and I'm going to go to 12 00:00:30,510 --> 00:00:34,350 apple.com, and forward 13 00:00:34,350 --> 00:00:37,200 slash rss, and I'm going to have a look at 14 00:00:37,200 --> 00:00:39,300 the Top 10 Free Apps which was the RSS 15 00:00:39,300 --> 00:00:41,370 we've been working on. And there's the 16 00:00:41,370 --> 00:00:44,070 XML, as you can see there. So we can close 17 00:00:44,070 --> 00:00:46,200 down or collapse any tags we're not 18 00:00:46,200 --> 00:00:47,910 interested in, by clicking on these 19 00:00:47,910 --> 00:00:51,840 little triangles here. And that can help 20 00:00:51,840 --> 00:00:53,730 us to remove some of the clutter, and 21 00:00:53,730 --> 00:00:56,250 give us a better feel for the data. So 22 00:00:56,250 --> 00:00:58,020 I'm going to collapse this first entry 23 00:00:58,020 --> 00:01:00,420 tag here and we can see that it's 24 00:01:00,420 --> 00:01:01,620 followed by another, and if I keep 25 00:01:01,620 --> 00:01:10,439 collapsing them, you probably won't be 26 00:01:10,439 --> 00:01:11,729 surprised to see that there's 10 of them 27 00:01:11,729 --> 00:01:16,470 in all. So let's go ahead and do that, and 28 00:01:16,470 --> 00:01:19,140 there's the 10 all collapsed. So these 29 00:01:19,140 --> 00:01:21,000 entry tags contain the information on 30 00:01:21,000 --> 00:01:23,220 each of the applications that are in the 31 00:01:23,220 --> 00:01:26,610 Top 10 Free Apps feed. Now all this stuff 32 00:01:26,610 --> 00:01:28,619 at the top is the information about the 33 00:01:28,619 --> 00:01:31,140 feed itself, so there's a title for the 34 00:01:31,140 --> 00:01:34,860 iTunes Store Top Free Apps, and the 35 00:01:34,860 --> 00:01:37,640 author's name in iTunes Store, down here. 36 00:01:37,640 --> 00:01:40,049 And we can see when the feed was last 37 00:01:40,049 --> 00:01:42,270 updated, but we're not going to be using 38 00:01:42,270 --> 00:01:43,979 any of that in our app - we're going to be 39 00:01:43,979 --> 00:01:46,170 concentrating on the information in the 40 00:01:46,170 --> 00:01:49,320 actual entry tags. So I'm going to expand 41 00:01:49,320 --> 00:01:52,020 one of these entry tags again, and we can 42 00:01:52,020 --> 00:01:53,579 see the individual fields that we're 43 00:01:53,579 --> 00:01:56,340 going to be using. Now the information 44 00:01:56,340 --> 00:01:58,649 we're interested in from the feed is the 45 00:01:58,649 --> 00:02:01,649 name, the artist, release date and 46 00:02:01,649 --> 00:02:04,920 summary. Now the entries also contain 47 00:02:04,920 --> 00:02:07,049 links to an image for the app, and we're 48 00:02:07,049 --> 00:02:08,639 going to add a field to store that as 49 00:02:08,639 --> 00:02:11,250 well. Now we won't be displaying the 50 00:02:11,250 --> 00:02:13,800 image in this app, but having the URL 51 00:02:13,800 --> 00:02:15,870 will make it easier if we want to add 52 00:02:15,870 --> 00:02:17,460 the image once we've seen how to 53 00:02:17,460 --> 00:02:19,350 do that. So what we're going to do is 54 00:02:19,350 --> 00:02:21,390 parse out the individual fields from the 55 00:02:21,390 --> 00:02:23,910 XML, so what I'm going to do is start by 56 00:02:23,910 --> 00:02:26,640 creating a basic class that we can use 57 00:02:26,640 --> 00:02:29,310 to hold the information. Now this class 58 00:02:29,310 --> 00:02:31,020 is really no more than a way to store 59 00:02:31,020 --> 00:02:33,210 the individual entries that we extract 60 00:02:33,210 --> 00:02:36,660 from the XML. Now I'm not saying you 61 00:02:36,660 --> 00:02:38,070 should do this and I'm not saying you 62 00:02:38,070 --> 00:02:40,260 shouldn't, but I'm going to put our Feed 63 00:02:40,260 --> 00:02:43,290 Entry class in the same file as Main 64 00:02:43,290 --> 00:02:45,960 Activity. So I'm doing this again just to 65 00:02:45,960 --> 00:02:48,660 show you that I can. Kotlin doesn't have 66 00:02:48,660 --> 00:02:51,060 the same restriction as Java, so there's 67 00:02:51,060 --> 00:02:53,640 no requirement that a class must be 68 00:02:53,640 --> 00:02:56,250 in a file with the same name. And because 69 00:02:56,250 --> 00:02:57,900 this is such a simple class, it's a good 70 00:02:57,900 --> 00:03:00,390 way to demonstrate that. But one thing I 71 00:03:00,390 --> 00:03:03,150 would suggest, is if you put a very small 72 00:03:03,150 --> 00:03:05,370 class in the same file as a major class - 73 00:03:05,370 --> 00:03:07,530 and MainActivity's pretty major, 74 00:03:07,530 --> 00:03:09,690 it's where the app starts - then put it at 75 00:03:09,690 --> 00:03:11,790 the top. If you put it at the end of the 76 00:03:11,790 --> 00:03:14,100 file, it's very easy for people to not 77 00:03:14,100 --> 00:03:16,350 notice it's there. And I would also 78 00:03:16,350 --> 00:03:18,390 suggest that if a class takes up most of 79 00:03:18,390 --> 00:03:20,130 the screen, it really should go in 80 00:03:20,130 --> 00:03:23,070 its own file. But as far as Kotlin is 81 00:03:23,070 --> 00:03:25,440 concerned, it makes no difference either 82 00:03:25,440 --> 00:03:27,960 way, so we're just considering people who 83 00:03:27,960 --> 00:03:29,880 may come along afterwards, to read and 84 00:03:29,880 --> 00:03:32,040 maintain our code. And remember that that 85 00:03:32,040 --> 00:03:33,390 person could well be you, 86 00:03:33,390 --> 00:03:35,850 months down the road. Of course, if you 87 00:03:35,850 --> 00:03:37,500 work for a company whose style guide 88 00:03:37,500 --> 00:03:40,290 prohibits putting more than one class in 89 00:03:40,290 --> 00:03:41,610 each file, then you'd want to obviously 90 00:03:41,610 --> 00:03:44,100 follow their guidelines. Alright, so 91 00:03:44,100 --> 00:03:45,210 what I'm going to do is go back to 92 00:03:45,210 --> 00:03:46,230 Android Studio, 93 00:03:46,230 --> 00:03:47,910 and making sure that I've got Main 94 00:03:47,910 --> 00:03:50,640 Activity.kt opened, and I'm 95 00:03:50,640 --> 00:03:52,320 going to come up here and add the class 96 00:03:52,320 --> 00:03:55,860 entry, starting on line 9. So I'm gonna 97 00:03:55,860 --> 00:04:01,350 start with class FeedEntry, and we add 98 00:04:01,350 --> 00:04:03,920 the opening and closing curly braces. And 99 00:04:03,920 --> 00:04:06,180 we need to add some fields, so it's going 100 00:04:06,180 --> 00:04:11,040 to be var name colon string equals, and an 101 00:04:11,040 --> 00:04:14,120 empty string, two double quotes, var 102 00:04:14,120 --> 00:04:19,140 artist colon string equals two empty, or 103 00:04:19,140 --> 00:04:22,980 two double quotes, var releaseDate. 104 00:04:22,980 --> 00:04:26,730 So releaseDate string equals two double 105 00:04:26,730 --> 00:04:31,530 quotes, var summary colon, you guessed it, 106 00:04:31,530 --> 00:04:33,480 string equals, in two double quotes, and 107 00:04:33,480 --> 00:04:35,970 finally, the imageURL, that's also gonna 108 00:04:35,970 --> 00:04:41,630 be a var, imageURL - that's also a string - 109 00:04:41,630 --> 00:04:44,040 and we're initializing it to an empty 110 00:04:44,040 --> 00:04:47,070 value. So as I said, it's a very simple 111 00:04:47,070 --> 00:04:50,100 class, and it just holds properties for 112 00:04:50,100 --> 00:04:51,870 the data that we're going to extract 113 00:04:51,870 --> 00:04:54,570 from the RSS feed. Now each time we 114 00:04:54,570 --> 00:04:56,490 create a new instance of FeedEntry, 115 00:04:56,490 --> 00:04:58,590 it'll store the details from one of 116 00:04:58,590 --> 00:05:00,720 those entry tags that we just looked at 117 00:05:00,720 --> 00:05:03,360 in the browser. Now when you create a 118 00:05:03,360 --> 00:05:05,490 class, it's often a good idea to create a 119 00:05:05,490 --> 00:05:08,550 toString method that can be used to 120 00:05:08,550 --> 00:05:10,080 print out a representation of a class 121 00:05:10,080 --> 00:05:10,590 instance. 122 00:05:10,590 --> 00:05:12,900 Now here it'd be useful to display some 123 00:05:12,900 --> 00:05:14,940 of the fields. Now we can actually get 124 00:05:14,940 --> 00:05:17,130 Android Studio to generate the basic 125 00:05:17,130 --> 00:05:19,410 stub for us. So to do that, we want to 126 00:05:19,410 --> 00:05:21,870 make sure that the cursor is at the end 127 00:05:21,870 --> 00:05:23,220 of the class but before the closing 128 00:05:23,220 --> 00:05:25,710 brace - I'm going to put it there - and then 129 00:05:25,710 --> 00:05:29,940 we use Ctrl-o to generate the code. We 130 00:05:29,940 --> 00:05:31,200 want to come down here and choose to 131 00:05:31,200 --> 00:05:33,630 String, to get Android Studio to create a 132 00:05:33,630 --> 00:05:35,610 basic toString method for us. So I'm 133 00:05:35,610 --> 00:05:38,670 just going to press ENTER there, and you 134 00:05:38,670 --> 00:05:40,620 can see it's created that function for 135 00:05:40,620 --> 00:05:43,680 us. Now part of the reason for including 136 00:05:43,680 --> 00:05:45,690 this, is to show you a neat feature of 137 00:05:45,690 --> 00:05:47,550 Kotlin when it comes to dealing with 138 00:05:47,550 --> 00:05:49,740 strings. We've already used template 139 00:05:49,740 --> 00:05:51,390 expressions, also known as string 140 00:05:51,390 --> 00:05:53,370 interpolation, and we're going to use 141 00:05:53,370 --> 00:05:55,740 some of those here. But Kotlin also 142 00:05:55,740 --> 00:05:57,560 provides an easy way to create 143 00:05:57,560 --> 00:06:00,810 multi-line strings. Now we do that by 144 00:06:00,810 --> 00:06:03,600 using triple quotes. So what I'm going to 145 00:06:03,600 --> 00:06:05,970 do first is delete the super call in our 146 00:06:05,970 --> 00:06:09,870 toString function, and that will just 147 00:06:09,870 --> 00:06:11,580 return the default details in our class, 148 00:06:11,580 --> 00:06:13,410 and we're going to add some extra code 149 00:06:13,410 --> 00:06:15,830 instead. So I'm going to type return, 150 00:06:15,830 --> 00:06:21,300 three double quotes, enter, name equals 151 00:06:21,300 --> 00:06:26,100 dollar name, enter, artist equals dollar 152 00:06:26,100 --> 00:06:30,120 artist, enter, releaseDate 153 00:06:30,120 --> 00:06:36,580 equals dollar releaseDate and image 154 00:06:36,580 --> 00:06:42,280 URL equals dollar imageURL. Then we're 155 00:06:42,280 --> 00:06:43,750 going to finish that with three triple 156 00:06:43,750 --> 00:06:49,720 quotes, like so. So the neat thing about 157 00:06:49,720 --> 00:06:52,090 that, using triple quotes in this way, is 158 00:06:52,090 --> 00:06:53,230 that we're going to get each property 159 00:06:53,230 --> 00:06:55,780 printed out on a new line. Now because 160 00:06:55,780 --> 00:06:58,240 we've formatted the code with spaces to 161 00:06:58,240 --> 00:07:00,370 line everything up, we also get a load of 162 00:07:00,370 --> 00:07:02,980 spaces at the start of each line. Now 163 00:07:02,980 --> 00:07:04,810 that's not a huge problem but it's 164 00:07:04,810 --> 00:07:07,600 easily fixed - we just tell Kotlin to trim 165 00:07:07,600 --> 00:07:11,050 off the indentation. So to do that, at the 166 00:07:11,050 --> 00:07:14,020 end of the closing triple quotes, we type 167 00:07:14,020 --> 00:07:19,270 dot trim indent - we're adding a call to 168 00:07:19,270 --> 00:07:21,580 the trimIndent function. Now that will 169 00:07:21,580 --> 00:07:23,200 only work if you put name on a new 170 00:07:23,200 --> 00:07:25,840 line, like I've done here. If it was 171 00:07:25,840 --> 00:07:28,210 on the line above, it would have no 172 00:07:28,210 --> 00:07:30,400 indentation and trimIndent uses the 173 00:07:30,400 --> 00:07:33,190 line with the smallest indent, to work 174 00:07:33,190 --> 00:07:36,010 out how many spaces to delete. Now we're 175 00:07:36,010 --> 00:07:37,510 leaving the user interface for this app 176 00:07:37,510 --> 00:07:40,240 until last, which means we need 177 00:07:40,240 --> 00:07:42,070 some way to tell that everything's 178 00:07:42,070 --> 00:07:44,500 working okay. So in case you hadn't guessed, 179 00:07:44,500 --> 00:07:45,850 we're going to use this toString 180 00:07:45,850 --> 00:07:48,100 function, to display our entries in the 181 00:07:48,100 --> 00:07:51,190 log. Now every object has a toString 182 00:07:51,190 --> 00:07:53,290 method that's used to provide some sort 183 00:07:53,290 --> 00:07:55,870 of textual representation of the object's 184 00:07:55,870 --> 00:07:58,570 instances. Now by default, it just shows 185 00:07:58,570 --> 00:08:00,870 the name of the class and the instances 186 00:08:00,870 --> 00:08:03,520 hashcode, which is just a number that 187 00:08:03,520 --> 00:08:06,700 uniquely identifies each instance. But by 188 00:08:06,700 --> 00:08:09,250 overriding the toString method, we can 189 00:08:09,250 --> 00:08:11,170 get a more useful representation of each 190 00:08:11,170 --> 00:08:13,180 object, so that our log will make a lot 191 00:08:13,180 --> 00:08:15,640 more sense. Alright, so that's the Feed 192 00:08:15,640 --> 00:08:18,190 Entry class created. Every time we get a 193 00:08:18,190 --> 00:08:19,990 new entry in the data, we're going to 194 00:08:19,990 --> 00:08:22,630 create a new FeedEntry object, and set 195 00:08:22,630 --> 00:08:24,880 the fields to the values that we find in 196 00:08:24,880 --> 00:08:28,090 the XML data. We now need to create the 197 00:08:28,090 --> 00:08:30,850 code to parse the XML - in other words, to 198 00:08:30,850 --> 00:08:32,140 find the various fields that we're 199 00:08:32,140 --> 00:08:34,210 interested in and extract the values. 200 00:08:34,210 --> 00:08:36,250 We're then going to store those values, 201 00:08:36,250 --> 00:08:38,169 as I mentioned, in the individual 202 00:08:38,169 --> 00:08:41,229 FeedEntry objects. Now the good news is 203 00:08:41,229 --> 00:08:43,059 that we don't have to write loads of 204 00:08:43,059 --> 00:08:43,690 code 205 00:08:43,690 --> 00:08:45,490 to go through that stream of XML, and 206 00:08:45,490 --> 00:08:47,200 checking for the less than and greater 207 00:08:47,200 --> 00:08:49,600 then symbols to work out where the tags 208 00:08:49,600 --> 00:08:51,760 are, then working out the values and so on. 209 00:08:51,760 --> 00:08:53,590 Now we could do all that if we really 210 00:08:53,590 --> 00:08:56,200 wanted to, but parsing XML is a very 211 00:08:56,200 --> 00:08:58,600 common thing for an app to do, so there 212 00:08:58,600 --> 00:09:01,000 are libraries included in Java that take 213 00:09:01,000 --> 00:09:03,820 care of all of that for us. Now there are 214 00:09:03,820 --> 00:09:05,710 some Kotlin libraries, for parsing XML, 215 00:09:05,710 --> 00:09:08,230 starting to appear, but I don't want to 216 00:09:08,230 --> 00:09:10,300 add external libraries to our projects 217 00:09:10,300 --> 00:09:10,990 just yet. 218 00:09:10,990 --> 00:09:12,910 And also, the Java libraries have been 219 00:09:12,910 --> 00:09:15,130 tried and tested for many years, so we 220 00:09:15,130 --> 00:09:17,770 know that they work well. Alright, so 221 00:09:17,770 --> 00:09:19,540 the library will deal with working out 222 00:09:19,540 --> 00:09:22,300 where the tags are, and which bits of the 223 00:09:22,300 --> 00:09:26,080 XML are the actual values, and so on. We 224 00:09:26,080 --> 00:09:27,790 can then use the library to read the 225 00:09:27,790 --> 00:09:30,790 values for the tags that we want. So what 226 00:09:30,790 --> 00:09:31,840 we're going to do is create another 227 00:09:31,840 --> 00:09:34,240 class to handle the parsing. Now I'm 228 00:09:34,240 --> 00:09:35,770 going to put this one in its own file, 229 00:09:35,770 --> 00:09:37,540 and I'm going to do that by right-clicking 230 00:09:37,540 --> 00:09:40,150 the package up here - academy.learn 231 00:09:40,150 --> 00:09:42,550 programming.top10downloader - 232 00:09:42,550 --> 00:09:45,810 right-click and click on New, and select 233 00:09:45,810 --> 00:09:49,800 Kotlin class, Kotlin File/Class. 234 00:09:49,800 --> 00:09:52,650 We're going to call this one Parse 235 00:09:52,650 --> 00:09:56,470 Applications, but don't press OK just yet, 236 00:09:56,470 --> 00:09:58,510 because notice that the kind of thing 237 00:09:58,510 --> 00:10:01,150 that we're creating here is a file. Now 238 00:10:01,150 --> 00:10:02,560 that's fine - everything will still work 239 00:10:02,560 --> 00:10:04,810 if you leave it at that, but you have to 240 00:10:04,810 --> 00:10:07,570 type in the class declaration. So instead, 241 00:10:07,570 --> 00:10:08,980 what we're going to do is change that to 242 00:10:08,980 --> 00:10:11,920 class first, and Android Studio will 243 00:10:11,920 --> 00:10:14,680 then do so of the typing for us. Now the 244 00:10:14,680 --> 00:10:16,630 icon's also a bit different for Kotlin 245 00:10:16,630 --> 00:10:19,360 files and classes. That may be useful to 246 00:10:19,360 --> 00:10:21,580 help you identify the types of file in 247 00:10:21,580 --> 00:10:23,920 the project pane. Now if the file only 248 00:10:23,920 --> 00:10:27,310 contains a single class, it has that Ck 249 00:10:27,310 --> 00:10:29,920 icon. If you then add another class or 250 00:10:29,920 --> 00:10:32,020 some code outside the class, it changes - 251 00:10:32,020 --> 00:10:34,500 like the icon showing for MainActivity. 252 00:10:34,500 --> 00:10:37,120 So I'm going to click OK now and we'll 253 00:10:37,120 --> 00:10:41,110 get our new class created. Ok, so you can 254 00:10:41,110 --> 00:10:43,089 see Android Studio has created that 255 00:10:43,089 --> 00:10:46,210 class for us. Now it's always useful to 256 00:10:46,210 --> 00:10:48,100 write log entries to see what's going on, 257 00:10:48,100 --> 00:10:49,780 so I'm going to start by adding our 258 00:10:49,780 --> 00:10:58,830 usual log tag. 259 00:10:58,830 --> 00:11:01,420 Now we're going to build up a list of 260 00:11:01,420 --> 00:11:04,060 FeedEntry objects. These will be created 261 00:11:04,060 --> 00:11:06,670 as we pass each of the entry tags in the 262 00:11:06,670 --> 00:11:08,650 XML, and will be stored in an ArrayList. 263 00:11:08,650 --> 00:11:10,110 So I'm going to call this one 264 00:11:10,110 --> 00:11:17,310 applications. So we do val applications 265 00:11:17,310 --> 00:11:21,670 is equal to ArrayList, then diamond 266 00:11:21,670 --> 00:11:26,290 operator, FeedEntry. Then outside the 267 00:11:26,290 --> 00:11:27,520 diamond operator, we're going to have 268 00:11:27,520 --> 00:11:30,610 parentheses. Alright, so that's the basic 269 00:11:30,610 --> 00:11:32,860 setup done. What we need now is the 270 00:11:32,860 --> 00:11:35,200 function that'll parse the XML data and 271 00:11:35,200 --> 00:11:37,690 create the list of applications from it. 272 00:11:37,690 --> 00:11:41,100 Now I'm going to call this function parse, 273 00:11:41,100 --> 00:11:43,200 because that's what it's going to do. 274 00:11:43,200 --> 00:11:46,510 Let's go ahead and create that. So it's 275 00:11:46,510 --> 00:11:50,890 going to be fun parse, and in parentheses, 276 00:11:50,890 --> 00:11:55,650 xmlData colon String and then colon 277 00:11:55,650 --> 00:11:59,980 boolean. Then left and right curly braces 278 00:11:59,980 --> 00:12:02,680 to define the function. And the actual 279 00:12:02,680 --> 00:12:04,510 code, we're going to start by putting a 280 00:12:04,510 --> 00:12:09,310 Log.d parentheses TAG comma. Then it's 281 00:12:09,310 --> 00:12:12,610 going to be parse called with dollar 282 00:12:12,610 --> 00:12:17,920 sign xmlData. We're going to put var 283 00:12:17,920 --> 00:12:20,550 status equals true on the next line. 284 00:12:20,550 --> 00:12:25,020 Next line, var inEntry equals false. 285 00:12:25,020 --> 00:12:29,110 Next line, var textValue equals, an 286 00:12:29,110 --> 00:12:31,900 empty string - two double quotes. So this 287 00:12:31,900 --> 00:12:33,730 function takes a single argument which 288 00:12:33,730 --> 00:12:36,459 will be the string to parse. Now I've 289 00:12:36,459 --> 00:12:38,350 made this function return a boolean 290 00:12:38,350 --> 00:12:40,720 value. If there are any problems, it will 291 00:12:40,720 --> 00:12:43,270 return false, to indicate that the data 292 00:12:43,270 --> 00:12:45,339 couldn't be passed for whatever reason. 293 00:12:45,339 --> 00:12:48,130 The variable status is initialized to 294 00:12:48,130 --> 00:12:50,709 true, and will be set to false if an 295 00:12:50,709 --> 00:12:51,690 exception is thrown. 296 00:12:51,690 --> 00:12:54,040 Now the code will also have to make sure 297 00:12:54,040 --> 00:12:56,800 that we're processing the data inside an 298 00:12:56,800 --> 00:12:59,110 entry tag. Now that's important because - 299 00:12:59,110 --> 00:13:01,660 and if I switch back to the XML in Chrome 300 00:13:01,660 --> 00:13:05,050 here - you can see that a name tag can 301 00:13:05,050 --> 00:13:07,510 appear outside of the entry 302 00:13:07,510 --> 00:13:10,780 tags, as this is the case up here. But it also 303 00:13:10,780 --> 00:13:14,260 appears in here as well. We've got the 304 00:13:14,260 --> 00:13:16,900 name here as well. So basically, you can 305 00:13:16,900 --> 00:13:18,400 appear within the author tag as well as 306 00:13:18,400 --> 00:13:20,470 inside an entry. Now we don't want to go 307 00:13:20,470 --> 00:13:22,510 adding that name - the first one outside 308 00:13:22,510 --> 00:13:24,940 of the entry tag - into a FeedEntry 309 00:13:24,940 --> 00:13:27,610 object. So to deal with that, we'll keep 310 00:13:27,610 --> 00:13:28,960 track of whether we're looking at the 311 00:13:28,960 --> 00:13:31,960 tags inside an entry or not. And 312 00:13:31,960 --> 00:13:33,670 getting back to Android Studio - that's 313 00:13:33,670 --> 00:13:36,970 using this variable in entry. Now text 314 00:13:36,970 --> 00:13:39,220 Value - that's used to store the value of 315 00:13:39,220 --> 00:13:41,770 a current tag, so that we can assign it 316 00:13:41,770 --> 00:13:44,020 to the appropriate field of our Feed 317 00:13:44,020 --> 00:13:47,110 Entry object. And in terms of processing, 318 00:13:47,110 --> 00:13:48,760 we're going to wrap up that processing 319 00:13:48,760 --> 00:13:50,800 inside a try block. So let's go ahead 320 00:13:50,800 --> 00:13:54,790 and do that - try. We're going to do a 321 00:13:54,790 --> 00:14:03,960 catch parentheses e colon exception, and 322 00:14:03,960 --> 00:14:05,920 we're going to put e.print 323 00:14:05,920 --> 00:14:08,230 StackTrace, if we do get an exception, and 324 00:14:08,230 --> 00:14:10,450 set status equal to false, as I mentioned. 325 00:14:10,450 --> 00:14:12,490 And then outside that we're going to 326 00:14:12,490 --> 00:14:16,990 return status. Now I haven't specified any 327 00:14:16,990 --> 00:14:19,390 particular exception here, so if anything 328 00:14:19,390 --> 00:14:21,490 goes wrong, any type of exception will be 329 00:14:21,490 --> 00:14:24,010 caught. If that happens, the status will 330 00:14:24,010 --> 00:14:26,140 be set to false, and we're printing out a 331 00:14:26,140 --> 00:14:28,540 stack trace to help us diagnose what 332 00:14:28,540 --> 00:14:30,490 went wrong. Now we're gonna return the 333 00:14:30,490 --> 00:14:32,290 status, which will be false if there was 334 00:14:32,290 --> 00:14:34,480 an error, otherwise it'll be unchanged 335 00:14:34,480 --> 00:14:37,900 from its initial value of true. Alright, 336 00:14:37,900 --> 00:14:39,220 so the next few lines of code we're 337 00:14:39,220 --> 00:14:42,160 going to add, set up the xml classes that 338 00:14:42,160 --> 00:14:43,180 are part of Java. 339 00:14:43,180 --> 00:14:44,860 So let's go ahead and do that, and that's 340 00:14:44,860 --> 00:14:47,560 going to be within the try block. I'm 341 00:14:47,560 --> 00:14:48,520 gonna type val 342 00:14:48,520 --> 00:14:53,920 factory equals xmlPullParserFactory - 343 00:14:53,920 --> 00:14:55,680 I'm selecting that from the drop down - 344 00:14:55,680 --> 00:15:00,100 dot newInstance. On the next line, 345 00:15:00,100 --> 00:15:04,660 factory.isNameSpaceAware - we're 346 00:15:04,660 --> 00:15:07,930 sitting that equal to true - and we're 347 00:15:07,930 --> 00:15:10,570 going to do the next line, val xpp 348 00:15:10,570 --> 00:15:17,410 equals factory.newPullParser. The 349 00:15:17,410 --> 00:15:18,970 line after that, we're going to do xpp 350 00:15:18,970 --> 00:15:20,899 dot setInput. 351 00:15:20,899 --> 00:15:26,600 I'm gonna set that to xmlData.reader. 352 00:15:26,600 --> 00:15:30,290 Then we're going to do var eventType 353 00:15:30,290 --> 00:15:34,949 equals xpp.eventType, var current 354 00:15:34,949 --> 00:15:41,850 Record is equal to FeedEntry, and then 355 00:15:41,850 --> 00:15:45,570 we're going to put while parentheses 356 00:15:45,570 --> 00:15:48,540 eventType is not equal to xmlPull 357 00:15:48,540 --> 00:15:54,089 Parser.END_DOCUMENT, and 358 00:15:54,089 --> 00:15:55,880 I'm gonna leave that part empty for now. 359 00:15:55,880 --> 00:16:00,240 So lines 22 through 24 - they're 360 00:16:00,240 --> 00:16:01,980 responsible for setting up the Java 361 00:16:01,980 --> 00:16:04,110 XML parser, that's going to do all the 362 00:16:04,110 --> 00:16:06,779 hard work of making sense of the XML for 363 00:16:06,779 --> 00:16:09,149 us. Now the important thing is to 364 00:16:09,149 --> 00:16:11,399 know how to use the parser - it's not 365 00:16:11,399 --> 00:16:14,459 necessary to know how it works. Now I'm 366 00:16:14,459 --> 00:16:15,839 going to explain the way it's used in 367 00:16:15,839 --> 00:16:17,610 our code here, but if you actually want 368 00:16:17,610 --> 00:16:19,560 to learn more about PullParsers, then 369 00:16:19,560 --> 00:16:21,240 you'll find plenty of information online. 370 00:16:21,240 --> 00:16:25,170 Now the PullParser site - I'll just 371 00:16:25,170 --> 00:16:30,199 briefly bring that up in a browser now - 372 00:16:30,199 --> 00:16:33,740 you can see there, it's available at 373 00:16:33,740 --> 00:16:37,290 www.xmlpull.org. That explains about it, 374 00:16:37,290 --> 00:16:38,970 and what's more, it also contains the 375 00:16:38,970 --> 00:16:41,569 link to the Java API documentation 376 00:16:41,569 --> 00:16:44,880 towards the bottom of the page. And the 377 00:16:44,880 --> 00:16:46,709 other thing to note is, there's also 378 00:16:46,709 --> 00:16:49,319 Android documentation available. We'll 379 00:16:49,319 --> 00:16:51,480 briefly go and have a look at that - this 380 00:16:51,480 --> 00:16:53,730 is for the xmlPullParser again - and 381 00:16:53,730 --> 00:16:55,949 that gives details on all the constants 382 00:16:55,949 --> 00:16:58,319 and methods available. Now you can't just 383 00:16:58,319 --> 00:17:00,240 go creating instances of PullParser 384 00:17:00,240 --> 00:17:02,730 objects as you need them. So instead, if 385 00:17:02,730 --> 00:17:05,339 we go back to our code, the API provides 386 00:17:05,339 --> 00:17:07,230 a factory that will produce a Pull 387 00:17:07,230 --> 00:17:10,049 Parser object for you. Now class 388 00:17:10,049 --> 00:17:12,059 factories are common in Java, and are 389 00:17:12,059 --> 00:17:13,559 often used when you don't know the 390 00:17:13,559 --> 00:17:16,380 actual class that's going to be used. Now 391 00:17:16,380 --> 00:17:18,270 we don't really care which class will 392 00:17:18,270 --> 00:17:20,040 provide the parsing functionality that 393 00:17:20,040 --> 00:17:22,799 we need - all we're interested in, is that 394 00:17:22,799 --> 00:17:26,069 it, in fact, can parse XML. So as long as the 395 00:17:26,069 --> 00:17:27,809 object we get back recognises the 396 00:17:27,809 --> 00:17:31,100 methods defined in the XMLPull API, 397 00:17:31,100 --> 00:17:34,100 our code will actually work with it. 398 00:17:34,100 --> 00:17:35,510 Now although you can dig a lot deeper 399 00:17:35,510 --> 00:17:38,330 into how all this works, and investigate 400 00:17:38,330 --> 00:17:41,150 all the various functions available, the 401 00:17:41,150 --> 00:17:42,830 beauty of using the supplied Java 402 00:17:42,830 --> 00:17:44,810 class is that you don't actually have to do 403 00:17:44,810 --> 00:17:48,050 that, in order to get results. Once you 404 00:17:48,050 --> 00:17:50,060 know how to create the objects, you can 405 00:17:50,060 --> 00:17:51,650 just use them without worrying about 406 00:17:51,650 --> 00:17:54,500 what they're doing behind the scenes. Now 407 00:17:54,500 --> 00:17:55,580 obviously, when you get into more 408 00:17:55,580 --> 00:17:57,110 advanced stuff, then you may want to 409 00:17:57,110 --> 00:17:59,420 understand what's happening more, but 410 00:17:59,420 --> 00:18:00,860 when you're just starting out, it's okay 411 00:18:00,860 --> 00:18:03,260 to just use the built-in classes to get 412 00:18:03,260 --> 00:18:05,720 the job done. So by the time we get to 413 00:18:05,720 --> 00:18:08,840 line 25, we have a valid PullParser 414 00:18:08,840 --> 00:18:11,870 object, so we can tell it what to parse by 415 00:18:11,870 --> 00:18:14,330 giving it a StringReader, that's using 416 00:18:14,330 --> 00:18:17,300 the xmlData String. Now a StringReader, 417 00:18:17,300 --> 00:18:18,980 by the way, is a class that treats a 418 00:18:18,980 --> 00:18:21,740 String like a stream. So think of it 419 00:18:21,740 --> 00:18:23,180 as an efficient way of processing 420 00:18:23,180 --> 00:18:26,180 strings, and the way that the XMLPull 421 00:18:26,180 --> 00:18:28,400 Parser does things. Now you probably saw 422 00:18:28,400 --> 00:18:30,050 it pop up that it wanted a Reader when I 423 00:18:30,050 --> 00:18:32,420 typed the opening bracket after set 424 00:18:32,420 --> 00:18:35,300 Input. So the XMLPullParser needs a 425 00:18:35,300 --> 00:18:37,580 StringReader to read from, and a String 426 00:18:37,580 --> 00:18:39,980 Reader needs a String to read - which is 427 00:18:39,980 --> 00:18:43,310 what we've got on line 25 there. Kotlin 428 00:18:43,310 --> 00:18:45,380 makes that a lot easier. It provides an 429 00:18:45,380 --> 00:18:47,060 extension function that returns a String 430 00:18:47,060 --> 00:18:49,520 Reader for us, already set to read from 431 00:18:49,520 --> 00:18:52,910 our xmlData string. Alright, so let's 432 00:18:52,910 --> 00:18:55,010 finish the video here now. In the next 433 00:18:55,010 --> 00:18:56,000 video, we're going to talk a bit more 434 00:18:56,000 --> 00:18:57,620 about the PullParser and also the 435 00:18:57,620 --> 00:19:00,380 Reader, just to clarify, and then move on 436 00:19:00,380 --> 00:19:02,210 to adding more code to our parse 437 00:19:02,210 --> 00:19:04,370 function. So I'll see you in the next 438 00:19:04,370 --> 00:19:06,610 video.