In the previous article, I showed you how to fetch records via GET request using retrofit(For part 1 click here) . In this article, I am going to show how to create a new record using POST request.
For this tutorial, I am using https://jsonplaceholder.typicode.com/
Now we will add a function to our API Interface to create a post.
Now create a function in HomeRepository called as createPost
Next, call this function from the viewmodel class
Now let’s create a menu file to display create post menu
Next, we will show this menu in Main Activity and on click of Create Post menu we will open a dialog in which we ask for title and body of new post.
Now we have to create a addData() function in HomeAdapter
And we are good to go. Now we can create a new post and add it our recyclerview.
Next in the third part of this series, I will demonstrate you to implement DELETE method using retrofit. Tap this link to reach there.
Stay tuned for the final part!!
For complete code, go checkout this github repository