Retrofit in Android Studio using Kotlin | Android Knowledge

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to my channel in this video we will learn about retrofit it might sound like a tough topic but trust me it is very simple I'll be releasing this video in Hindi as well on Android knowledge 2.0 channel so make sure to subscribe my second Channel as well Link in the description box now let's understand what is retrofit this is all bookish definition let me tell you what exactly retrofit is retrofit is a library which is used for making Network request and handling API Communication in applications basically it communicates between web server and application web server is an API now what is API API stands for application programming interface it provides a set of rules and protocols that allow different applications to communicate with each other enabling developer to access and utilize functionality from external services within their own applications in very simple language consider I'm creating an app like Ola Uber where I need map functionality so either I have two options to create my own map API or simply use Google Maps API in my application Google Maps API is an external service provided by Google which we can use in our application API can be free or paid we have several API like news API weather API chat JD API Spotify API and many more the all works in real time means all the information provided by API are live like consider I'm creating a News application where I'll use news API so because of that I'll get all the latest news in my app getting my point now there is something called as rest API let's understand it so rest stands for representational State transfer it's a part of API that basically has a set of HTTP based protocols and principles that defines how system can communicate over the Internet they use http methods or annotations like get post p delete and many more out of which we will mostly use get post and query method get method is used to retrieve the data post method is used to send data to the server query method is used when we have to specify a particular parameter we will use all of this when we will create project okay I know Theory might sound very boring to you but these are few important terms that you need to know before creating an example project next is Json Json stands for JavaScript object notation it is basically a format in which the data is preserved data is in maybe all the latest news or weather updates got it but code cannot understand Json formant hence we are supposed to convert it or in technical language we say pass the data serialize or deserialize it there are many ways to convert Jon format to data out of which we will be using Json Library I repeat Jon not Json we will add Json dependency in Android Studio that will help us to convert it got it that converted data will be a for class that is plain old Java object or simply you can say a data class now let's have a look at API as I'm creating an example project only H I'll be using a fake API we will use real API as well in the end of this series but for now this is a popular website Json placeholder do typing code.com that students and developer use to learn API let's go to this website first here it is this is not API this is a website that is providing us an API then where is API these are all the apis that website is providing US Post comments album Todo let's have a look at them now out of all of them I'll be going for albums API album as in song albums let's have a detail look at it here two things you need to know first is the URL and second is Json syntax this is our URL till here it's a base URL and this one is an endpoint that is basically our API itself see Json placeholder is a main or base URL that provide us an API and this albums is an end point that is an API itself got it this is important because in code we have to mention base URL and endpoint to access the service also we can further customize URL as well by categorizing it more but for now just remember this pattern then this is what Json formats look Alik I know it looks confusing but you will be able to understand it once you are aware with the Json syntax so let's have a look at it also you'll be wondering what is this language so it's let in it won't bother us okay so now see this is what the syntax looks like we have two square brackets in the start and in the end those square brackets is array okay and inside array we currently have two objects in it object is determined by the curly braces so this is one object and this is another object both of them are separated using a comma then in each object we have a key and a value separated by a colon both of them are in double quotes this is key and this is value for example this is an array inside it two objects and then inside each object object we have key and values this ID is a key and one is an INT value then this name is a key and Android is a string value then in second object ID is the key and two is an INT value and name is a key and knowledge is a string value got it there can be multiple key and values in an object and multiple objects in an array got it now let's have a look at Json format see this square bracket is an array that starts here and ends here in between they have multiple object to be precise they have 100 objects separated by comma and in each object we have keys and values so in first object we have key as user ID and value as one then another key as ID and value as one then another key as title and value as some string next we have another object with same case but different values and so on so basically we are going to send a request through retrofit to this API or server and as a response it will send us this Json response which will convert it in data using Json library and display it on the text view simple right let's quickly have a look at all the steps so first we will add dependency then permissions then we will create a data class and interface and retrofit instance then from all the above steps we have sent the request and now we we will receive it and display it in the text view now let's go to Android studio and create it choose empty views activity name it as learn retrofit and finish first as I said we will add dependency so go to grel module these are the three dependency I'll mention them in the description box this is for retrofit this is for Json library for conversion and this one is for live data we will use live data to receive the request I have already explained live data in detail in one of my previous video you can click on the eye button to watch then add view findinding as well also I do face error with compile SDK so I change it to 34 and now click on sync now and done then next as all of this process is happening on internet so obviously we will require internet and network permissions so go to Android manifest first I'll add Internet permission then next I'll add Access Network State permission and then next I'll add Access Wi-Fi State permission and that's it now we need to create a data class so right click on it new class name it as album item and choose data class here we are supposed to add keys with their data type that were present in objects and Json I'll keep it side by side see key these are user id id and title right so I'll write here as well ID as int then user ID as end and then title as string simple then I'll add a serialized name to each one of them now what is serialized name the serialized name is an annotation in Json which is used to map a Json key to a data field during serialization or deserialization basically it allows you to specify a custom name for the key if it differs from the data field name to ensure a proper conversion between Json and data safe play then we have keys values we will receive it from server but if you'll have a look at it you will see all these objects are in an array so we are supposed to create that array too right so come back to Android studio right click on it create a new cotlin class name it as albums as I said we have to create an are list and inside it the object so I'll extend it as array list and inside it our data class that is album item itself right and that's it now our next step is to create an interface so right click on it create a new class name it as album service and choose interface here we are supposed to Define end point using https method that is get method so I'll write here as get annotation and inside it write the end point remember I told you about the base URL and Endo this is our Endo so I'll write here as slash albums basically get matter retrieves the data right so to retrieve it I'll create a get albums function and as we are retrieving it we will use response class it's a part of retrofit and inside it album class it retrieves a list of albums simple right now next step is to set up retrofit instance it is very easy it's almost mostly syntax only so right click on it create a new class name it as retrofit instance here we are supposed to define the base URL and initiate retrofit so to do that first I'll create a companion object companion object consists of Singleton object basically like a static keywords then inside it create a variable named as main URL and insert here the base URL that is the website that is providing the API I'll copy paste it and add then below it we will create a get retrofit instant function that will return a retrofit so first let me quickly write it and done see it will return retrofit with its base URL as the URL we have mentioned above and then Json converter that will convert the Json into data and finally build it once everything is set up we will receive the request through live data and display it in the text view so first we need to give a text view an ID and go to activity m here add IDs title text view because we will receive entire data but we will display only the title for now I'll remove it and done then go to main activity here as I said we will receive the request through live data so let's create it first let's set up binding and done then next we need to initialize the service so let me quickly write it first and done this line creates an instance of an interface that is album surveys using retrofit basically it enables you to make HTTP request to the/ albums that is our end point defined in the interface then next with the help of live data we will take the response so let me quickly write it [Music] and done now see this code defines a live data that is response live data that emits the response from the retrofit service. getet albums and itn't sends the response it is typically used for observing and handling the responses then next we need to observe that data so first let me quickly write it and then see this code observes the response live data and upon receiving a response it trads through the list of albums in the response body and store it in the album list variable then if the album list is is not null means it is present then use while loop to iterate through each album and store it in album item variable and then for each album it extracts the title and store it in album title variable and then finally append it to the title text view append is to add we usually use appen instead ofex in web services okay and that's it our code is ready let's run the app perfect all the album title are displayed on the screen see compare it Side by side I know it looks messy but it's an example project just to make you understand how retrofit Works further in upcoming videos we will create a professional app using retrofit but for now just remember the steps to implement retrofit okay also for more updates you can follow us on Instagram or join our telegram group Link in the description box so yeah that is it for the video If you new to this channel then please consider subscribing to my channel and I'll see you in the next video [Music]
Info
Channel: Android Knowledge
Views: 3,070
Rating: undefined out of 5
Keywords: retrofit android, retrofit 2 android tutorial, retrofit 2.0 android tutorial, retrofit path, retrofit, retrofit query, retrofit path and query, retrofit android example tutorial, retrofit get, retrofit query list, retrofit path variables, retrofit query array, android, retrofit querymap, retrofit url query, retrofit url manipulation, retrofit url parameters, retrofit url example, retrofit get request, retrofit path parameters, retrofit query parameters, android retrofit 2
Id: KJSBsRKqNwU
Channel Id: undefined
Length: 19min 5sec (1145 seconds)
Published: Fri Oct 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.