Posts

Showing posts with the label Retrofit 2

Fix: Adding GSONConvertorFactory to retrofit2 while calling an API

 When using Retrofit 2 to call an API and you want to handle JSON data, you can use the GsonConverterFactory to serialize and deserialize JSON data. Here's how you can add it to your Retrofit setup: 1. First, make sure you have added the Retrofit and Gson dependencies to your project. You can do this by including the following lines in your app-level `build.gradle` file: ```gradle implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' ``` Ensure you use the appropriate version of Retrofit and Gson for your project. 2. Create a Retrofit instance with the GsonConverterFactory added. Typically, you would set up your Retrofit instance like this: ```java import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; Retrofit retrofit = new Retrofit.Builder()     .baseUrl("https://api.example.com/") // Your base API URL     .addConverterFactory(GsonConverterFactory.create())     .buil