Skip to main content

Posts

Showing posts from June, 2024

How to call Post 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. Step By Step Implementation Step 1: Create a New Project in Android Studio We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project. Step 2: Add the below dependency to your build.gradle File Navigate to the Gradle Scripts > build.gradle (Module:app) and add the below dependency in the dependencies section.  // below dependency for using the 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 AndroidManifes...

How to call Post type API using Retrofit in Android?

Step by Step Implementation Step 1: Create a New Project Step 2: Add the below dependency in your build.gradle file Navigate to the Gradle Scripts > build.gradle (Module:app) and add the below dependency in the dependencies section.  // below dependency for using the 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.   Step 3: Adding permissions to the internet in the AndroidManifest.xml file Navigate to the app > AndroidManifest.xml and add the below code to it.  <!--permissions for INTERNET--> <uses-permission android:name="android.permission.INTERNET"/> Step 4: Working with the activity_main.xml file Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.  <?xml version="1.0"...

Retrofit 2 — Receive Plain-String Responses

Android apps usually interact with REST APIs, which often use JSON as a data format. We've focused almost all of our tutorials on sending JSON or XML requests, and converting JSON or XML responses. Scalars Converter for Plain Strings To receive plain-text or plain-string responses you can utilize the scalars converter. You can integrate the converter by adding it as a dependency to your app's build.gradle: dependencies {       implementation 'com.squareup.retrofit2:retrofit:2.9.0'     implementation 'com.squareup.retrofit2:converter-gson:2.9.0'     implementation 'com.squareup.retrofit2:converter-scalars:2.5.0'//You need to add this     implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.2' } Next, you need to describe the endpoint you want to interact with. In this demo case, you'll use a GET request with the dynamic URL feature to pass any URL to the method, and set the response type as String. @GET() Call<String...