Android, Kotlin Retrofit GET, POST, PATCH, DELETE Http Methods implementation with RecyclerView MVVM

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to my channel in today's video i'm going to show you how you can implement get post patch and delete using the retrofit so let's begin to start with the adding dependencies so first of all i'm adding the retrofit second dependency we need the converter json for our retrofit another dependency we need for the view model so i am adding life cycle extensions and another dependency i'm adding to enable the logging in in the retrofit so i'm adding logging interceptors should be interceptor so using this we can enable the logging like we can print url headers and request response and since we are going to make the api call so i am adding the permission in our manifest and before we start with the ui so let's add let's implement the retrofit instance and retrofit service so this class will basically return a function or the instance of the retrofit and we need to pass the base url so i found a portal where we can they have some options to call getpost patch and delete so i'm just using the same url so there are few options you can see so i'm using the base url and that is basically mentioned here the first one so i'll be using this but will pass users from as a repository from our retro service class and here we'll add the converter json and as we added a dependency to enable the logging so i am now enabling the logging for to do print request your response and headers and we'll pass this client in our retrofit we'll use the same client dot build so this is the way we can enable logging for our retrofit now i am adding another class or interface for our retro service and here as we already know we define some functions with their http method so first function i'm creating it to get user list so this function will return the list of users and for that we need to define a data class so let's create first data class i am giving it as named user list and let's see how what data this url is returning so let's see how data is look like so let's uh let's copy this and format it properly so that it should be readable so you see the data name email gender status so it should be a area of data types and now i'm creating another data class which is user type and there will pass will keep the same name we missed the id so let's get the id also and few more parameters we'll use the same name what is defined in the response let's keep the id on as a first parameter and now this get user list will return the list of users user type and we need this url needs some headers also so we'll pass headers as well from here it should be headers and will pass both the headers what is defined inside the url and these headers should be coma separated and that's it so this is the way we pass the headers for our api now i'm creating another function to create a user which is going to be the post request and similarly we'll pass all the headers which is needed for our post request and you see like the third header is the access token so just to get the access token you need to log create a account on this portal i already created an account so i have the access token i'm simply passing that access token in my request header so i'm currently hard coding it you can keep it in your project somewhere or you can generate it like some of the servers and give you at the run time but here i'm just hard coding and the body is going to be the user type which we already define in the user list data class and it's going to return some another data class that is i'm giving you at his name user response that should have code meta and a data of user type so we'll be using that user response type here another function will be needed to search i'm going to show you how you can type some text and make the api call and display on a recycler view so this search text will be getting from the edit text so this is going to be the another get api we are going to use so this is the first api this is going to be the second api which is going to take the search as the input parameter let's see how it would look like okay name equal to some value yeah so this url will like be this and another you are another request which is going to return just the user data based on the user id so will be this function i am going to use for the edit purple so will be passing the user id get the data show it in the edit tags and then user is updating and then it will update the same request so this is going to be the particular data based on the user id so this is how it's gonna be the response now we need another function for the patch like patches like whenever we are updating just the record data so when we are updating name like name email so by that time we'll be calling patch and similarly it need body path and we need another function to call delete so this function will simply delete that record but we don't need a body for that function now let's create our ui in the main activity so i'm simply adding one added text on the top and a button which is going to call search under that i'll be adding a recycler view and i'll be adding a button just to call the create user activity so recycler will be we will be having a user list and this button will call a great activity like use we can create a new user using the post so this button will call that so first function we need to initialize our recycler view which we already defined in our activity and as we already know for recyclerview we need the layout manager we need to add a decoration like separator between the rows and then we need a adapter so we'll be creating all these i'm creating a adapter for our recycler view okay and inside the oncreateview holder we just need to inflate the layout and at this point of time we need to create our layout file will pass the same name here now we'll design our layout so i'm simply adding three text view one for name one for email and one for status and now simply will set the data like name email and status now i'll set the same adapter to our recycler view in the main activity now we'll we should create a another class now for the view model so i'm creating a view model for our main activity so this immutable live data for um for the list on the recycler view so we need to create a function which is going to return this recycler list data and we need another function which is going to call our retrofit and the function we define inside our retro service interface we'll call get user list in this one and inside our main activity now we'll initialize our view model you if there is no return then we'll simply show a toast and when we receive the data we'll simply set it to the adapter and now we'll call this init viewmodel function we'll create another function for handling the search so if added text like search input box is empty then it will simply load the whole list else it will make the api call to get the result based on the parameter enter in the edit text i'm creating a separate function inside our viewmodel and we'll call the same function which you define inside our retro service and it will call what user has entered it will call now the same function from the main activity let's run and see how it look like so we are able to retrieve the list but it's it's useful we made the same mistake like match parent you need to change it in our recycler list xml so now we are able to retrieve the list that's great now let's see how search look like it's not returning any result let's see as i said like we are enabling the logging in our retrofit so this is how we can print the logs we can see the request response headers everything so let's see what's the mistake we made so there are no result for that particular text so it's taking search name equal to search okay so there is a mistake we are passing that button here instead of add a text id so i'll change it here now rerun and see i am typing there you go so search is also working now so let's change the tags there you go so search is working we are able to retrieve the data so get method is successfully implemented now i'm creating a activity for create new user and there will implement a post function first so i'm adding added tags for answering a name and email and this is for the email so i'm just going to take two inputs as of now you can add as much as possible and the button which is going to call create and another function which is where we will be implementing the delete user so i'm adding that button as well here we need to create another view model for our create new user activities i am creating another view model for implement post patch and delete as usual since we have created a view model so we need to initialize it in our activity so i am calling that copying that code from the main activity and putting it inside over create new user activity now we'll be creating same observers so one light data we need for create new user that's going to be the user response type i'm use copying the same function and will be modifying it and instead of get user list we'll be calling create user function and instead of user it's user list it should be user response it should be the create user this function now we'll call this function from our create new user activity this should be the user response and similarly we'll show toast if there is no response we'll call our toast fail to create a new user else will show toast and we'll say successfully created a user new user and simply will close this activity actually let me put it inside the different function we'll call it a great user observable and now we'll implement now we'll handle this create button click listener and this create user function will be called on the button click so here we need to build the user object first so first thing is the id we need to pass second is what user entered in the edit text and third parameter is what user entered in the email added text and it take two more parameter so i'm just hard coding those status i am sending active and in gender i am setting it as mail now i will call the create user function of our viewmodel will pass this user object now let's run and see so we'll call on this create button it's not doing anything i think we forgot to [Music] handle this button click in our main activity so let's handle this create button so we'll simply start this activity we'll call this activity create new user on the create button click it's crash let's see what we miss so button name is create user button so we use the different id now let's run and see there we go create is opening a new activity let's give an enter some name and email id and call create button so it's actually let's see the logs i see the 200 201 so user is created successfully here let's search there so our post is implemented successfully now let me create another user i'm giving it as bbb created i'm searching it now there you go so post is also implemented successfully that's great now to implement patch or we can say edit we need to handle the recycler view click on the row click so that on row click we can call this create new user activity then we'll get the data and pre-fill the name and email and user can add it and then call the update button so let's handle the click first after using the adapter will build an intent and we'll call create new user activity and from this activity will pass the user id to the create new user activity so that based on that user id we can make a api call and get that user data and we'll pre-fill that edit text both add text and when we are modifying this any record any user at that time if we are reloading so our recycler view should get the updated record that's the reason i call start activity with dessert so here in the create new user activity we'll retrieve the user id first and we'll use the same name but we passed from the previous activity and we'll use the same and first thing based on the user id will load that user data that we have to check null because we are reusing the same activity so make sure we always have the null check because this activity does not contains user id when it is directly called to add a new user it is only contains the user id in case of edit and this function will to load the user data so when this observable will call so it will contain some data so we'll set it to our edit text name and email and at the same time we'll change the create button text to update so that when we are coming for create we should know we are coming for create and when we are coming to update record then we it should update and at the same time will be enabled will be changing the visibility of our delete button so in case of edit we can delete that record as well from the list it should be get user when get user currently have user list so it should be user response i should use the same live data load user data and we'll call it from our activity now so view model dot get our data and we'll pass the user id and the create function need to be changed now because the same function is going to call but it should be conditional if user id is equal to null then call create if it is not null then call update so we need to create another function to update now inside our view model and we'll be calling the same function update user and we'll pass the user now and similar change will make it here but update need the user id as well so we'll change add one more parameter you see it's need user id and the user object so user id for the url and user object is for the body so we pass that user and we need to pass the user id also and since we are going to use the same observable so let's change this method toast message also so i'm keeping it common you can have the separate observers also if you have to show some different messages so let's see now so i clicked on one data it get the data it set it to the edit text and now i'm done seeing that delete button so i'm changing it to visible now it was still gone over it there so it should be visible and i am putting some text calling update there you go so you see it's changed now and we are able to retrieve the updated data also i am putting it back there you go so it's reloading that's the reason i called on activity with result so start activity with result you see the it's reloading the whole list when we are coming back in case of editing there you go perfect now we need to implement the delete button functionality that is the last method so for that we i'm creating a another function to delete so it will simply delete a record from the list and i am creating another observable for delete so delete just need the user id it doesn't need a body so i removed it and we'll be calling this from our activity before that we need to set the observable and that is the delete and we'll get the get delete observable we don't need this i don't need a user it should be nullable we'll set it and once it need to handle the files just to show the text message so let's copy this and add it here and just will change the message successfully deleted user and we already call this function on the delete button click so let's see it's great on create let me show you post again i'm creating a record so create is working search is working i'm able to but i'm still not able to see delete button but it's updating the record so let's see okay so this id need to be changed to this it should be under the create button but they are on the same place so let's see it now and give it a name i think we removed it let's create one more time it's created in searching click we see the delete button now that's great so first time i'm showing you update it's updating now i'm going to call delete and we'll search it again it's not there that's great now it's loading whole list and creating one more time one more record it's created let's search it now it's there now you are able to see that i'm updating it update is working fine now i'm showing you delete delete is also working so guys that's all for today's video thank you so much for watching if you have any question you can ask in the comment section thank you
Info
Channel: LearningWorldz
Views: 5,926
Rating: 4.9354839 out of 5
Keywords:
Id: TJpk7ezvtGo
Channel Id: undefined
Length: 47min 41sec (2861 seconds)
Published: Mon Mar 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.