Retrofit - Send a simple GET Request | Android Studio Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
uh hello there and welcome back to my new video so with this video i'm going to start a new tutorial series about retrofit library so retrofit is a type safe http client for android and basically in this video we're going to send a simple get request and receive a response in the format of a json so we're going to use this rest api server to send the get request and the receiver response so we're going to use this endpoint uh posts uh slash one and uh basically when we open that we will see the response which we're going to get and here we just have a four fields a user id id title and body so we're going to send the get request to this endpoint and the receiver response in the json and then we're going to parse that json in our codeline object and display that results inside our application okay so let's open up our android studio project and here i have already added all the necessary dependency for a retrofit library for a json converter and for kotlin core routines as well so here we also have a viewmodel scope so that's all you need to add alright so the first thing we need to create a new package name the model and inside this package we're going to create a new model class named post okay so let's name this class post and this class will have only four different fields and this class will be basically a data class so let's add those four different fields so the first field is a user id the second id the four the third title and the last one body so the first two fields are are the type of integer and the last two are type of a string so the name of those variables needs to be the same as the response inside the json uh you can set the different uh variable name but then you would need to add the annotation on top of that variable and so for example let's add a different variable name and then we need to specify this annotation a serialized name and here we need to specify the actual uh id from a json response okay so that's how it works but this time i'm going to get back to uh this original example so as you can see that user id needs to be the same uh as in our in our response so i'm going to delete this annotation and just rename this variable to user id okay so the next thing i'm going to create a new package okay let's name this package api and inside this api let's create a new interface and we're going to name this interface simple api and here we're going to make a new function for ascending a get request so first let's add this get annotation and here we need to specify the exact endpoint so the end point in this case is a named posts slash one okay so just copy that and paste here inside this get annotation and down below let's create a new suspend function so we are using suspend keyword because later in the view model we're going to use the kotlin code routines and this function will return a post and the post is a model class which we have created earlier so uh the next thing we need to create a new package and this package will be named utils and inside this package we're going to create a new class named constants and this class will basically hold the one simple constant for our base url and inside the companion object we're going to create that constant okay and here let's paste this base url from our web browser so just copy this code this url and paste that inside so this is a constant and we're going to be able to access this constant from each and every class so next in api directory or api package we need to create a new object with the name of a retrofit instance so the reason why i use this object keyword because i want to make this retrofit instance as singleton so now let's create a new variable named the retrofit and let's lazily initialize our retrofit builder okay so just call a retrofit dot builder okay just let's uh import this uh retrofit okay and down below we need to specify uh base url so just type a base url and import that constant which we have created earlier okay uh next we need to specify a converter factory so we're going to use a json uh converter factory because uh i have chose that one and call a build function down below okay and below that we need to create the one more variable named api with a type of simple api and we are going to lazily initialize this retrofit so here we're going to basically call this create function on our retrofit and we're going to pass our simple api interface all right so that's all we need to do to create a retrofit in our object okay so the next thing i want to create a new package name the repository and inside this repository we are going to create a simple uh class which will represent a repository as well so just name this class repository okay and this class will hold the for now only one function so as well add this suspend keyword and let's name this function get post the same and it will return post and inside this function body we're going to call this retrofit instance to get the reference of this api and then to get the reference of this get post function from our simple api and we're going to return that okay all right so the next thing we need to create a view model okay so let's create a new view model named the main view model because this viewmodel will be used for our main activity so this class will extend the viewmodel and we're going to pass a one parameter inside our viewmodel and that is repository so later we're going to create a viewmodel factory so we can actually pass that parameter to our view model so you will see but for now let's create a new function here named get post and inside this function we're going to call this viewmodelscope to launch a kotlin core routine and here i'm going to create a new variable on top of that named my response and it will have the type of mutable live data of a post okay and here i'm going to create a new variable named response and i'm going to call this get post from the repository and then i'm going to put that response inside this mutable live data object and later we're going to use that mutable live data and we're going to observe it from our main activity but for now let's create a new uh and last class name the main viewmodel factory so this class will represent the viewmodelfactory and here is a parameter we need to specify a repository so let's type a private wall repository okay and we need to implement one method so uh just um move your mouse over to this warning and press alt plus enter and import this function so here we need to return uh our main view model and the pass repository and that's all we need to do for our me for our main viewmodel factory and that's all we need to create so now inside our main activity we're going to create the logic where we are going to uh observe the results from our mutable live data so you will see first let's uh initialize our view model here and let's initialize the repository as well down below let's initialize our viewmodel factory okay and here uh pass a repository as a parameter okay and down below let's initially initialize our viewmodel so here we need to pass the two parameters the first is the owner and the second is the viewmodel factory and that's how we are going to be able to pass this repository to our viewmodel and here just call this main viewmodel okay and down below we're going to call this function getpost from our view model and basically this get post will get the result from uh this function and we're going to store that result inside this immutable live data object and now inside our main activity we're going to observe that mutable live data object and we're going to basically parse the result so here let's name this result just a response okay and here i'm going to log the response of this of this get request okay so use this response to get the the fields user id id title and body okay okay and here and now we can run our application and see inside our lock it if we're going to get the results okay so we have an error and we forgot to add the internet permissions so just open uh android manifest file and here specify internet permissions so okay all of that so let's run our app again and now our application is working fine and when we open up a logcat we should be able to see result so as you can see the result is here and it is shown successfully okay so we have successfully made a simple get request and we have a basically logged that result so there is one more issue so for example let's uh rename this endpoint to something that does not exist okay so just remove this o character and now let's try to run our application and you will see that our application will crash and when we check our logcat we're going to see the uh http exception http 404 and that is a error code for page does not exist okay so how can we handle this exception very easily so we need to wrap this post inside the response and the response is is a part of retrofit library so here inside our repository inside simple api and inside the main view model just put this response or post inside a response okay so from our main activity now uh we're going to basically create an if block and we're going to call this response and its method is successful so only if this request is successful then we're going to display that results okay so here we're going to call this a body to actually get that or those fields and here i'm going to just put this double bang operator and in else block we can just log a different message saying for for example uh error or something else so here i'm going to get a response to get the error body and i'm going to just use this tool string function to convert that and here let's just put this title inside our text view in our app so we can actually see some of the response inside our application and in last block i'm going to just print this response code or error code in else block okay so now let's run our application and let's see uh how will our app look so now as a text view we can see a 404 error code and our application is not crashed it works perfectly fine and now let's open up this simple api and let's uh uh uh and let's get back that oh character so now we can get the actual results and as you can see our text view now contains the title from our request from our get request and basically everything is working perfectly fine so we have made a simple get request and we have received that response we have passed that json object to our kotlin object and we have displayed that to a user okay so that will be all for this video in the next few videos i'm going to also show you some of the interesting functionalities of this retrofit library so you will see how you can customize a url in different ways how you can add the custom queries to your endpoints and so on i'm going to also show you how you can add the results from that get a request inside a recycler view and maybe i'm going to show you also how you can use other methods like a post delete and so on so that'll be all for this video uh thanks for watching please like this video if you find it helpful of course and see the next one
Info
Channel: Stevdza-San
Views: 97,595
Rating: undefined out of 5
Keywords: retrofit, http, client, android, library, get, request, response, result, connect, to, internet, web, post, put, patch, delete, read, receive, data, from, send, how to, guide, tutorial, app, https, parse, json, kotlin, object, gson, converter, moshi, server, respond, display, in, builder, model, repository, class, instance, viewmodel, exception, url, uri, base
Id: sBCE_hOFnQU
Channel Id: undefined
Length: 12min 36sec (756 seconds)
Published: Sun Aug 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.