1 00:00:00,820 --> 00:00:06,010 Welcome back in this video, we're going to set up retrofit to our applications so that we can then 2 00:00:06,010 --> 00:00:09,070 actually load data from the news API. 3 00:00:09,400 --> 00:00:15,460 And what we're going to add now has nothing to do with the news API, but it's generally something that 4 00:00:15,460 --> 00:00:21,700 you need to do when you are working with retrofit and that is that you need to add the Gradle entry 5 00:00:21,730 --> 00:00:23,030 to your implementations. 6 00:00:23,050 --> 00:00:31,270 So here at the bottom in dependencies, you need to add your dependency for retrofit and the dependency 7 00:00:31,270 --> 00:00:33,150 is going to go something like this. 8 00:00:33,160 --> 00:00:34,900 It depends on the version that you're using. 9 00:00:35,140 --> 00:00:38,710 So come square up, retrofit to retrofit. 10 00:00:38,860 --> 00:00:41,260 In my case, the version is 2.0 9.0. 11 00:00:41,470 --> 00:00:48,370 So if you want to be short used, the same one that I'm using here and this will allow us to use retrofit. 12 00:00:48,940 --> 00:00:51,340 And then let's sink it really quickly. 13 00:00:51,610 --> 00:00:57,970 And once it is sink, let's look at an article that we prepared just for the general understanding of 14 00:00:57,970 --> 00:00:58,390 what's up. 15 00:00:58,720 --> 00:01:06,460 So at the right hand side, when we were on this page, you will see a live example of the URL and let's 16 00:01:06,660 --> 00:01:12,820 let's break it down, as I already talked about it a little bit, but let's see the basic URL that we 17 00:01:12,820 --> 00:01:16,180 have because we need to understand this as we are going to use it in retrofit. 18 00:01:16,510 --> 00:01:21,370 So we have the base URL, which is, for example, news API dot org slash v2 station. 19 00:01:21,730 --> 00:01:23,440 And then comes the absolute path. 20 00:01:23,440 --> 00:01:26,320 And you see GPS has to be part of the base URL as well. 21 00:01:26,710 --> 00:01:31,330 So then we have the absolute path here, which, for example, would be the top headlines. 22 00:01:31,540 --> 00:01:37,780 So if we want to have top headlines only then we would search for specifically top headlines and everything 23 00:01:37,780 --> 00:01:39,490 that comes after the question mark. 24 00:01:39,700 --> 00:01:43,990 Article three parameters such as the country where you then set it, for example, to the US. 25 00:01:44,290 --> 00:01:52,210 Then if you have additional query parameters that you want to set, you put an eye and sign. 26 00:01:52,720 --> 00:01:58,120 So this and sign, for example, is required in this case because we also always need to add the API 27 00:01:58,120 --> 00:01:58,360 key. 28 00:01:58,360 --> 00:02:03,460 So API key equals and then you would add your own API key, whatever that was. 29 00:02:04,090 --> 00:02:08,949 So the response that we're getting is going to be in the form of JSON, and I talked about it just very 30 00:02:08,949 --> 00:02:09,370 briefly. 31 00:02:09,669 --> 00:02:14,500 But generally speaking, it's the JavaScript object notation that we are using. 32 00:02:14,500 --> 00:02:19,420 So that's where the Sun comes from Java script, object notation. 33 00:02:19,420 --> 00:02:23,610 So maybe it's easier if we if I put it like this, j. 34 00:02:24,070 --> 00:02:24,550 So. 35 00:02:25,160 --> 00:02:25,510 Hmm. 36 00:02:26,080 --> 00:02:26,500 OK. 37 00:02:27,010 --> 00:02:28,000 That is for the web. 38 00:02:28,420 --> 00:02:32,770 Well, since Android doesn't understand this form, what we need to create, what it can understand, 39 00:02:32,770 --> 00:02:38,290 which is a data class that will serve as a template for the type of data we are expecting from the API. 40 00:02:38,620 --> 00:02:40,690 So let's understand this a little better. 41 00:02:41,110 --> 00:02:43,180 So we have the curly brackets, right? 42 00:02:43,480 --> 00:02:49,840 And everything that is inside of curly brackets denotes an object so an object can have an object inside 43 00:02:49,840 --> 00:02:50,510 of it as well. 44 00:02:50,530 --> 00:02:52,340 So you see, we have this bigger object. 45 00:02:52,340 --> 00:02:58,570 This is that big JSON objects, which gives us the status, which gives us the total results, for example. 46 00:02:58,570 --> 00:03:03,970 So this is a parameter which gives us, for example, the status which would be one member or one variable 47 00:03:04,090 --> 00:03:10,480 of our object that says, OK, so it's a string, then we have the total results, which is going to 48 00:03:10,480 --> 00:03:11,440 be a number. 49 00:03:11,440 --> 00:03:12,610 So it's going to be an integer. 50 00:03:12,940 --> 00:03:18,790 And then we have articles which will be, as you see here with the square brackets and array or a list. 51 00:03:18,940 --> 00:03:21,400 So this is a list of objects once again. 52 00:03:22,030 --> 00:03:29,470 So here we have another object, which then contains a source, which itself is an object which has 53 00:03:29,470 --> 00:03:32,770 the property of ID and not a which can be null. 54 00:03:33,190 --> 00:03:37,090 Then we have the name which would, for example, be a string in this case, YouTube. 55 00:03:37,540 --> 00:03:42,490 Then we have the author, which, for example, would be null if we don't have any details about it. 56 00:03:42,850 --> 00:03:43,870 Then we have the title. 57 00:03:44,050 --> 00:03:48,450 So this is part of the next property rights of these. 58 00:03:48,460 --> 00:03:49,660 Each time is a new property. 59 00:03:49,660 --> 00:03:51,910 So so this is a property which is an object. 60 00:03:51,910 --> 00:03:56,020 But then we have the next property, which is author, and the next one is this title and the description, 61 00:03:56,020 --> 00:04:01,690 which is pretty long, which is a long string URL, which is a string the image to the URL, which is 62 00:04:01,690 --> 00:04:05,530 a string when it was published, it is a string and then the content, which would also be string. 63 00:04:05,950 --> 00:04:11,950 So we just need to bring this format now this JSON format, which is a very easily readable format for 64 00:04:11,950 --> 00:04:18,370 us as humans but at the same time can be very easily extracted into objects that are understandable 65 00:04:18,370 --> 00:04:19,930 for our program. 66 00:04:20,440 --> 00:04:24,130 So that's really amazing and I love it, and that's why we're going to use it. 67 00:04:24,640 --> 00:04:28,630 And you should love it as well, because it's going to allow you to build all kinds of applications. 68 00:04:29,260 --> 00:04:31,030 OK, so what do we do with all of this? 69 00:04:31,300 --> 00:04:33,670 Well, now we need to create models of it. 70 00:04:34,090 --> 00:04:39,550 So we have this data class that is going to be called top news response where we get the status, which 71 00:04:39,550 --> 00:04:41,440 is a string, but that it can be null. 72 00:04:41,470 --> 00:04:44,140 So we're going to set it to null and it will be available. 73 00:04:44,500 --> 00:04:48,910 Then we have the total results, which is an integer which will be null and then we have articles which 74 00:04:48,910 --> 00:04:50,950 will be a list of top news article. 75 00:04:51,430 --> 00:04:56,680 So now we need to define top news articles and we need to create another class which will create a well, 76 00:04:56,680 --> 00:04:59,890 contain all of those top news article. 77 00:04:59,990 --> 00:05:05,590 Contents, which itself has a sauce, which has to be an object and then has a bunch of properties. 78 00:05:05,830 --> 00:05:06,790 So let's look at that. 79 00:05:06,800 --> 00:05:11,140 We see we have a sauce, which will be a sauce class that we need to create. 80 00:05:11,470 --> 00:05:15,400 Then we have the author, the title, the description and so forth. 81 00:05:15,880 --> 00:05:20,080 And then the sauce, as I said, is the class itself because it has an ID and a name. 82 00:05:20,440 --> 00:05:24,260 So it's because it's inside of these curly brackets. 83 00:05:24,280 --> 00:05:32,500 The source basically says, OK, I am of type object, so let's go ahead and go over to our project 84 00:05:32,500 --> 00:05:35,980 and create all of these data classes because we are going to require them. 85 00:05:36,220 --> 00:05:38,970 So that's generally the approach that you take. 86 00:05:38,980 --> 00:05:41,920 So you look at what is going to be the response that you're going to get. 87 00:05:42,220 --> 00:05:47,890 And then you create the data classes or the models for your data that you're going to receive because 88 00:05:47,890 --> 00:05:52,780 you need to get the JSON data converted into readable objects for your project. 89 00:05:53,080 --> 00:05:55,810 And then you can use it like normal objects in Kotlin. 90 00:05:56,470 --> 00:06:03,100 After we have added the retrofit independent dependency and we clicked on sync, now let's go ahead 91 00:06:03,100 --> 00:06:08,680 and create all of those different data models that we need to create. 92 00:06:09,850 --> 00:06:16,360 The first one being the top news response, so we need to create a new package here. 93 00:06:16,450 --> 00:06:22,850 It's always good if you do that and you call it models, so all of our models are going to go in here. 94 00:06:23,450 --> 00:06:24,550 We have a model already. 95 00:06:24,550 --> 00:06:27,040 I think news data, you could say, is also a model, right? 96 00:06:27,580 --> 00:06:36,190 So now let's go ahead and create a new Cortland class file and it will be a data class. 97 00:06:36,550 --> 00:06:39,700 So let's call this one top news response. 98 00:06:41,130 --> 00:06:47,970 OK, so this will contain, for example, as I stated earlier, the world contained a status. 99 00:06:49,550 --> 00:06:52,850 The results and the top news articles. 100 00:06:53,960 --> 00:06:58,800 OK, so this is what we need to set up, so we get this from our response, right? 101 00:06:58,820 --> 00:07:05,120 So this is the top news response and then we get the status, the total result and then the actual articles 102 00:07:05,810 --> 00:07:11,030 and the articles are going to be a list of something that we are going to call top news article. 103 00:07:13,160 --> 00:07:18,200 Because what I want to have is only displayed the top news articles. 104 00:07:18,440 --> 00:07:24,800 So I'm going to create a new class, create a separate file, which will be in the same destination. 105 00:07:25,830 --> 00:07:29,130 And this will be a class I'm just going to call this one data class. 106 00:07:29,190 --> 00:07:34,870 So this will be a top news article, and here I need to define what kind of parameters. 107 00:07:34,890 --> 00:07:40,860 Top news article has, and I'm just going to paste them in here because otherwise, while you will just 108 00:07:40,860 --> 00:07:45,450 look at me typing all the time, it's the same parameters that we get from here, right? 109 00:07:45,570 --> 00:07:48,300 Source, author, title description and so forth. 110 00:07:48,540 --> 00:07:50,700 And you can also get it from their documentation, right? 111 00:07:50,700 --> 00:07:52,260 You don't have to read it in my article. 112 00:07:52,620 --> 00:07:56,910 We we saw it earlier here, right where we had the articles, which at the source, the author, the 113 00:07:56,910 --> 00:08:00,870 title description URL you're able to image, publish it and content. 114 00:08:01,410 --> 00:08:03,690 So we need all of those in our project. 115 00:08:04,050 --> 00:08:06,210 So that's what we have here. 116 00:08:07,350 --> 00:08:10,650 But then the source, as you saw, was also an object. 117 00:08:10,860 --> 00:08:16,620 So we need to create a class and here unfortunately, there is actually a Swiss class, so don't import 118 00:08:16,620 --> 00:08:20,010 it, but instead create a new class called source. 119 00:08:21,130 --> 00:08:27,460 OK, so I'm going to create and extract it to a separate class, which will be inside of models as well. 120 00:08:27,790 --> 00:08:32,110 So this will also be a data class and the source data class is very simple. 121 00:08:32,110 --> 00:08:34,929 It really just has an ID and a name. 122 00:08:35,440 --> 00:08:37,539 So let's put this into brackets. 123 00:08:38,669 --> 00:08:44,190 And it will be like this ID, which will be of type, string, syllable and name, which will be a type 124 00:08:44,190 --> 00:08:44,880 string Nullarbor. 125 00:08:47,210 --> 00:08:48,830 OK, so that's our models. 126 00:08:49,220 --> 00:08:50,720 And now comes the interesting part. 127 00:08:51,140 --> 00:08:56,210 So now we need to create something that is going to be our news service. 128 00:08:56,810 --> 00:09:02,270 OK, so this will be an interface which will help us to send out the requests. 129 00:09:02,960 --> 00:09:07,610 So let's go ahead and create a news service, which will be an interface. 130 00:09:08,360 --> 00:09:11,420 I'm going to call this one news service. 131 00:09:14,660 --> 00:09:17,240 All right, so what does this new service interface do for us? 132 00:09:17,960 --> 00:09:24,140 Well, for now, it's just going to have to retrofit to HDTV get request. 133 00:09:24,710 --> 00:09:29,120 So we need to get the data from the news API rights. 134 00:09:29,120 --> 00:09:35,870 In order to get it, we need to create a get request and we need to pass in the value here of what we 135 00:09:35,870 --> 00:09:36,470 want to get. 136 00:09:36,500 --> 00:09:40,890 So, for example, I want to only get top headlines, so the top news, so to speak. 137 00:09:41,630 --> 00:09:45,560 So then I need to create a function which will take care of that. 138 00:09:45,860 --> 00:09:48,290 It's going to be called get top articles. 139 00:09:48,980 --> 00:09:51,080 And here we need to pass in the query. 140 00:09:51,110 --> 00:09:53,960 So our search requests, so to speak. 141 00:09:54,560 --> 00:10:01,430 So here at Query, which is another retrofit to HTP annotation. 142 00:10:02,120 --> 00:10:07,010 And then we need to pass in what we want to pass as to the value. 143 00:10:07,040 --> 00:10:11,840 So for example, I want to search by a specific country. 144 00:10:12,740 --> 00:10:16,760 So I'm going to get the country as a string. 145 00:10:17,540 --> 00:10:19,490 Then I'm going to have another query. 146 00:10:20,450 --> 00:10:26,270 So here, another pet query keyword, which will be the API key. 147 00:10:27,020 --> 00:10:30,740 And this will be an API key of type string. 148 00:10:31,900 --> 00:10:32,440 Like so. 149 00:10:33,820 --> 00:10:39,670 And this will return a call to top news response. 150 00:10:43,390 --> 00:10:45,070 So we need to import call. 151 00:10:46,210 --> 00:10:48,430 Which is the one from an retrofit, too. 152 00:10:49,120 --> 00:10:50,500 And we need to import. 153 00:10:50,530 --> 00:10:53,560 Top news response, which is this one here? 154 00:10:54,130 --> 00:10:59,890 So basically what we are saying is please get me the top articles where the country is, whatever we 155 00:10:59,890 --> 00:11:03,940 are passing to you as the first parameter or is the country parameter. 156 00:11:04,420 --> 00:11:12,100 And then the API key that we're passing to you, as well as a string and return a couple of top news 157 00:11:12,100 --> 00:11:12,670 response. 158 00:11:13,180 --> 00:11:17,030 So it will give us this piece of data. 159 00:11:17,030 --> 00:11:22,690 So an object that will be of tip top news response, which will contain the the status, the total results 160 00:11:22,690 --> 00:11:26,350 of the amount of results and all of the articles that are inside of the top news. 161 00:11:29,920 --> 00:11:34,180 OK, and now it's good practice to have an API object. 162 00:11:34,660 --> 00:11:39,880 So let's go ahead and actually, let's put all of this stuff into a network. 163 00:11:40,060 --> 00:11:45,400 So we had models, we have the components, we have our UI and now let's go ahead and create the new 164 00:11:45,400 --> 00:11:48,640 folder or new package, which I'm going to call network. 165 00:11:49,300 --> 00:11:51,460 So here all of the network classes will go in. 166 00:11:51,850 --> 00:11:58,150 So I'm going to direct news service because it is off type while it is a network class or interface. 167 00:11:58,510 --> 00:11:59,770 So let's refactor it. 168 00:12:00,280 --> 00:12:05,230 So now we have the new service in there, but I will also create an object in here. 169 00:12:06,930 --> 00:12:14,670 Which will contain details about, for example, the base URL and then the retrofit builder and so forth. 170 00:12:15,150 --> 00:12:22,320 So let's call this one API like so this will just be an object, as I said, and we need to have the 171 00:12:22,320 --> 00:12:25,980 base URL, so we need to know which link we need to use. 172 00:12:26,370 --> 00:12:30,450 So it's always common to build it like this. 173 00:12:30,600 --> 00:12:36,270 You just create this constant, which will be a base URL when you're working with constants that's always 174 00:12:36,270 --> 00:12:38,790 common to use all caps letters. 175 00:12:39,060 --> 00:12:40,710 So this will be not changed. 176 00:12:41,790 --> 00:12:48,960 HTTPS forward slash forward slash news API dot org slash v to slash. 177 00:12:49,290 --> 00:12:51,150 OK, so this will be the base URL. 178 00:12:51,150 --> 00:12:53,670 You can find it here as well. 179 00:12:54,030 --> 00:12:58,110 So if you look at the base URL, you can see it here. 180 00:12:58,140 --> 00:12:59,100 This is the base here. 181 00:12:59,100 --> 00:13:01,860 All the way up to here is what is the base URL? 182 00:13:03,030 --> 00:13:08,370 And then you could either search for everything or you could search for top headlines. 183 00:13:08,730 --> 00:13:14,040 And we are specifically interested in top headlines, which is why we're using this thing also with 184 00:13:14,040 --> 00:13:15,000 the country code. 185 00:13:15,360 --> 00:13:18,690 So this was what we had said earlier in our get request. 186 00:13:18,870 --> 00:13:26,190 So here it's a get request with the base URL up to here, then with additional parameters that you need 187 00:13:26,190 --> 00:13:29,310 to pass, which we are going to do as well. 188 00:13:30,830 --> 00:13:33,110 OK, so now let's get back to our project. 189 00:13:34,340 --> 00:13:36,830 All right, so now let's initialize retrofit. 190 00:13:37,250 --> 00:13:45,990 Therefore, I'm going to create a retrofit object, which I'm going to use the retrofit builder for 191 00:13:45,990 --> 00:13:46,040 it. 192 00:13:46,970 --> 00:13:54,050 So this is a method that that retrofit offers, and I'm going to Paris into base URL or set the base 193 00:13:54,050 --> 00:14:00,020 URL better set with our base URL that we have set up earlier this one. 194 00:14:01,470 --> 00:14:02,070 And then. 195 00:14:03,670 --> 00:14:06,220 I built this like so built. 196 00:14:07,740 --> 00:14:13,080 So this will be my retrofit objects or initializing the retrofit object here, and then I can use it. 197 00:14:13,260 --> 00:14:15,950 So we'll now initialize the service class. 198 00:14:15,960 --> 00:14:24,360 So therefore we set up the retrofit service, which will be of type news service, which is the interface 199 00:14:24,360 --> 00:14:28,290 that we just created by lazily doing it. 200 00:14:28,800 --> 00:14:31,620 That's just common practice to use by lazy here. 201 00:14:32,100 --> 00:14:37,040 And then we create something with our retrofit object. 202 00:14:37,080 --> 00:14:40,050 So here, retrofit, thought, create. 203 00:14:40,620 --> 00:14:44,460 And here we create a new service class. 204 00:14:44,940 --> 00:14:45,390 Java. 205 00:14:45,480 --> 00:14:49,170 So that's what we need to pass to our create method. 206 00:14:52,180 --> 00:14:55,840 So first, we create a variable for the base URL, then we set up the retrofit. 207 00:14:56,320 --> 00:15:01,750 Then we add the base URL and build it, and then we initialize the service class using retrofit. 208 00:15:01,840 --> 00:15:04,660 So that's what we're doing in this API object for now. 209 00:15:07,240 --> 00:15:10,420 We will create a dedicated class to manage to request logic. 210 00:15:10,630 --> 00:15:15,340 But we are going to do that in the next video because I think this video is already long enough. 211 00:15:15,730 --> 00:15:17,260 So this was just a setup VIDEO. 212 00:15:17,560 --> 00:15:20,500 You see, this is quite advanced content. 213 00:15:20,500 --> 00:15:24,880 So it's not something where it can just build it in three lines of code and then show you something. 214 00:15:24,910 --> 00:15:30,010 So we need to build up a lot of stuff and then everything comes together, OK? 215 00:15:30,340 --> 00:15:33,510 So keep on following along and see you in the next video.