![]() |
VOOZH | about |
Android applications use APIs within Android Applications to access data from databases. We can perform several operations using APIs such as Reading, Writing, and Updating our Data in the Database. In this article, we will take a look at How to Update Data in API using Retrofit in Android using Jetpack Compose.
Note: If you are seeking Java code for Jetpack Compose, please note that Jetpack Compose is only available in Kotlin. It uses features such as coroutines, and the handling of @Composable annotations is handled by a Kotlin compiler. There is no method for Java to access these. Therefore, you cannot use Jetpack Compose if your project does not support Kotlin.
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Below is the dependency for Volley which we will be using to get the data from API. For adding this dependency navigate to the app > Gradle Scripts > build.gradle(app) and add the below dependency in the dependencies section.
// below dependency for using retrofit. implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
After adding this dependency sync your project and now move towards the AndroidManifest.xml part.
Navigate to the app > AndroidManifest.xml and add the below code to it.
Navigate to app > java > your app’s package name > Right-click on it > New > Java/Kotlin class and name your class as DataModel and add the below code to it. Comments are added in the code for further explanation.
Navigate to the app > java > your app’s package name > Right-click on it > New > Kotlin class select it as RetrofitAPI and add below code to it. Comments are added in the code for further explanation.
Navigate to the app > java > your app’s package name > MainActivity.kt file and add the below code to it. Comments are added in the code for further explanation.
Now run your application to see the output of it.
Output: