Android Retrofit Tutorial Using Kotlin 2023 | Getting JSON Response From Api Using Retrofit Kotlin

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign to get data from API using kotlin in your Android application so this is our project and we will be using this Json placeholder link I will put this link in the description below you can copy it from description and the first thing first we need to add these two dependencies for retrofit and the one for Json converter okay I will also put these dependency in the description below so you can copy it from the description and most important thing is we need to add the internet permission use this permission and simply type internet and in your manifest file to communicate with our API using the internet okay and here as you can see this is my API clause and here we have a get request and the end point is comments okay and I call it I call this function get comments and this this will return the list of comments and this is a call okay so this is the type of call and in this call we will be getting the list of comments from the API the Json placeholder API okay and this is our comments data class so here we have different kinds of variables like body email ID name and post ID widget you want to get from the API so in our main activity we will create a private function here private function get all comments and inside this function we will create our API call so API is equal to retrofit dot Builder okay dot builder then we'll type base URL so the base URL will be our base URL or the root URL which is uh defined here okay and now we will add the converter Factory so air converter Factory and simply type gs1 converter Factory dot create and we will build our request and then we will create our API so create and we will class the API class which is my API class dot Java okay so this is our retrofit request and now we will call this so to call this request in kotlin so simply type API Dot get comments okay and this get comments is coming from this my API class okay and now we will come back here and simply type Dot enqueue and type object and simply type call because we are getting the call this get comments is the type of call so import this and make sure you import the right one which is the rate of it too so simply click on enter and here type list and the list of which list of comments okay and now we will just simply add curly braces here and hit enter and if we uh check here so it it says Implement a member so simply click on the employment members and Okay so I think I made a mistake here remove it so it will be object it will be a call wake not a call I think yeah it will be a call back not a call so list and comments and add a curly braces here and now if we go to implement method or simply type uh move your cursor here and simply uh Alt Enter and Implement members and as you can see on response and on failure so simply select this two and now what we will do here in on response we will get our response and on failure we will catch some errors from the API or network errors and what I will do here I will just simply check here if response dot is successful then it means that we are getting 200 code from the API so simply type response dot body and we will add let function here and inside this let we will add a for Loop so for comment in it okay it means that this response body and inside this for Loop we will add a locket simply type log I and we have a tag here okay you can create your own tag or you can simply pass it hard coded here and in the on response uh which I will do okay I will get the comment body so simply type dollar sign and it dot sorry not it type comment dot body okay and on on failure what we will do we will simply create a log I also on failure and what we will do we will get the error T Dot message okay so if we got some error so we will catch it here and now I will run my application now if I go to the locket and I will just simply copy this tag check response and okay in the process is started but we getting any comments here and this may rerun it again to the locket okay okay sorry my bad I forgot to call this method and make sure you call this method when it is created so get all comments and now I will learn my app and if I go to the locket so you will notice we will get all the comments here okay unable to resolve please with the host name okay so we will check what's the issue here on failure and if I go to this link and working I think but it seems my internet is really slow and if I type simply slash comments so we should get the comments response yeah we are getting the comments response and uh what I will do I will rerun my apple game and now we will check if you're getting the comments or not this is due to internet yeah as you can see we are getting all the comments which are printed here in the log get so uh the relay was uh because of my internet which is very slow I know as you can see we are getting all the comments here okay and this is the comment uh comments body and now if I change it to something like email and I run my app again maybe it takes some time again for the getting the response you know okay so as you can see we are getting all the emails from the comments API so that's how you can get data Json data from your API using retrofit in your Android application with kotlin so if you like this video please share this video with your friends and also make sure you are subscribed to my channel and the bell notification option is on so you won't miss the upcoming videos
Info
Channel: CodingZest
Views: 17,137
Rating: undefined out of 5
Keywords: CodingZest, Android Retrofit Tutorial Using Kotlin 2023, android retrofit api tutorial, android retrofit tutorial, android retrofit kotlin, retrofit tutorial android kotlin, retrofit android tutorial kotlin, retrofit android tutorial using kotlin, retrofit android tutorial in kotlin, retrofit implementation in kotlin android, retrofit error handling android kotlin, retrofit json parsing android kotlin, retrofit kotlin android example, retrofit kotlin android studio
Id: sRLunCZX2Uc
Channel Id: undefined
Length: 9min 17sec (557 seconds)
Published: Thu Apr 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.