Retrofit Setup - MVVM News App

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to your new video in this video we will set up everything we need for a retro feel to be able to make Network requests the first step for setting up retrofit is always to create the response objects that we need so whenever we make a request with retrofit we will get the answer as a JSON string for those of you don't know Jason that is just a way to convert complex objects in a simple string that you can send over the network and then extract the complex objects again and we need to create cotton objects that represent the answer we will get from the news API we use luckily there is a super awesome Android studio plug-in called Jason to Kotlin class in which we can just paste a JSON string and it will directly output us the needed cotton object you can get this plugin by pressing ctrl alt and s to open up your Android studio settings and then we can navigate to plugins and click on marketplace and here you want to type Jason oops Jason to kirtland class this one I already have it installed but make sure to install this one you maybe have to restart in root studio but after that press ok and move to the website the news API dot orc website you can see on the home page of that there are some example requests and responses so that your L here is the request that we would do and this is the corresponding response so we make this request with some query parameters here for example we have the parameter Q here and in this request they set it to Bitcoin so we search for all news regarding Bitcoin and in the response object you can see that is the JSON string that is basically just a whole bunch of curly brackets and square brackets with basic data types you like strings we have null objects integers and so on and all we have to do to convert that JSON string to cotton is to copy that whole bunch of code here and paste it in our plugin but sadly on this website they have those little minus symbols here that also get copied but they don't belong to the actual request or actually responds to solve that problem we can just copy this URL the request URL and open it in a new tab and here get the the same rearm response again and you can just press ctrl + a to select everything and then copy it go back to Android studio right click on your project package our mvvm news app go to new and you can see we can create a cutland data class file from jason this one we want to select here and here we just paste the jason string that we copied from our web browser and we have to choose a class name for our response object i will just name this newest response and after that just click on generate and the plug-in will do the work for us you can see it generated three classes this one is our actual news response that response consists of a list of articles the status of that response and the number of total results and for those articles it generated and another class article that contains the author content and so on so now we are actually ready to create our API interface let's go to our mu news app and create a new package I call it API and in this API package we create a new Kotlin file class make sure to select interface here and call it news API this interface will be used to define our single requests that we can execute from code and the first function in that interface will be used to get all the breaking news from our API and whenever we make an HTTP request we need to specify the type of that require in this example because we want to get the breaking news from our API this is a get request because we want to get data from our API and for that we need to annotate our function with add yet and in the parentheses we need to specify the URL that we want to get data from and that is not the whole URL it's only the part after the base URL that we haven't specified yet but if you take a look in my browser then the type that we have to specify in the in the get parenthesis is this part after the base URL so news AAP idol org is the base URL and the part after that is the URL part that we want to get something from but we don't want to get the news from everything instead we only want to get the breaking news so we go back into Android studio and URL for that is actually V 2 slash chop - headlines and to find out that you just have to read the documentation of that news API that you can find all those URLs that you can make requests to and after that get annotation we finally want to create our function that gets our breaking news and because this is a network call function we want to execute that function asynchronously and the best way to currently do this is by using curry teens and to you to actually make that function able to be used in a crew teen we need to make it a suspend function a suspend function get breaking news and then we have to type in some parameters here and I want to be able to specify from which country we want to get the breaking news and for that we need to add parameters to this function and if this parameter is part of part of the request if that is actually a request parameter we need to annotate that parameter with add query and in the parentheses of that query we need to type the name of that parameter which you can also find in the documentation of the news API that is just country and I will name this variable country code because it's not the entire country it's just to let our country code and I will sell it by default to us then we will have another parameter also add query which is the page so that will later be helpful to paginate our requests because we don't get all the oddly breaking news at once because that would just be too much data at once instead we only get 20 articles at once and if we want to get the next 20 articles we will request the page 2 instead and if we want to get the 20 next articles after that we request page 3 and so on so that will be the page number which is an integer and I set it to 1 initially and we need to include our API key in that request so that the news API actually knows who makes that request so we write add query which is API key and I will also call this premier API key which is a string and I will set it initially to our constant variable API key which I haven't created yet I actually want to create that in a separate class for constants so let's create that go to mvvm news app i will create a new package here and call it util for utility classes and inside of this new tow package I create a cuddling class call it constants select class here instead of this class I create a companion object so we don't need to create an instance of that class and here I will create a reference which is a conce Val for our API key and here I will paste my API key and normally you should never publish your API key like I do it here but that is only because I don't really care about what happens with this API key because I won't need it anyways after I finish this tutorial but whoever has your API key can make requests with it in your name basically and that means if you pay for an API for example then he can make requests and you have to pay a lot for requests you didn't make so never publish your API key if you really need it in a real project then we can go back into our news API interface import API key here and this function needs to return something and that is a response a response of type news response because that is the response object that was generated from that jason-2 cutting class plug-in and we also need to improve response of course from the retrofit library here and not from ok HTTP 3 make sure to select the first one then we can copy that whole block of code here and paste it below for our search requests so whenever we want to search for news we will change the URL of that to everything now because we want to search for all articles available and not only breaking news articles I will name this function search for news and instead of the country parameter we both pass a Q instead for query I guess that work that we can just search for a particular string I will call this search query which is a string and I don't give it a default value here so we move that us part and the rest actually stays the same for this function and finally what we need to do is we need to create our retrofit singleton class that enables us to make requests from everywhere in our code so let's do that in our API package right click new cotton file class select class here and I will call it retrofit instance then press Enter and inside here we will create a companion object and we will create a private Val retrofit by lazy this lazy just means that we only initialize this here wants what we will put in this in these curly brackets so first of all I told you in the first part of this course that I oughtn't a dependency to be able to log responses of retrofit which is very useful for debugging I will create that logging interceptor it is called so Val logging is equal to HTTP logging interceptor we will just attach this to our retrofit object to be able to see which requests we are actually making and what the responses are then we need to set the logging level so logging the offset level and we set it to http logging interceptor without level dot body so we actually see the body of our response so the actual response and then we can use that interceptor to create a client a network line which is an okay HTTP HTTP client builder and here we can add that interceptor and pass our login here logging and call the build afterwards and now we can use the client and pass it to our retrofit instance so we will return in this lazy block retrofit dot builder first we want to set the base URL to face your l which is also a constant I want to create in our constants class so I'll go back here and create accounts Val base your L and that is just HTTP news API org then we can go back to a retrofit instance import base URL then add a converter factory to our retrofit that is just used to determine how the we response should actually be interpreted and converted to cotton objects and we will use the jisun converter factory here which is just the the google implementation of Jason converting and now we have to call jisun converter fact we'd create here and we want to add our client which we added above don't forget to call build afterwards and that's it for a retrofit builder now I can finally get our API instance from that retrofit builder by creating a valid API also by lazy and inside of this we return a retrofit dot create and inside here we need to pass the class of our API of our interface which is News API double colon class to Java and this is the actual API object that we will later be able to use from everywhere to make our actual network requests so I hope this video helped you to understand retrofit and how to actually set it up if so please let me know in the comments and if you didn't understand something then please let me also know that I can answer your questions have a good day see you next video bye bye [Music] [Music]
Info
Channel: Philipp Lackner
Views: 15,920
Rating: 4.9925232 out of 5
Keywords: tutorial, android, development, learning, programming, programmer, kotlin, beginner, mvvm, architecture, clean architecture, retrofit, room, coroutines, coroutine, navigation components, network, networking, http, request, response, api
Id: dr-CFz1j_8k
Channel Id: undefined
Length: 14min 34sec (874 seconds)
Published: Wed Apr 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.