Retrofit helps your app talk to the internet easily. It turns web data into Kotlin objects so you can use them in your app.
Retrofit setup in Android Kotlin
val retrofit = Retrofit.Builder()
.baseUrl("https://yourapi.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()baseUrl is the main web address your app will use.
addConverterFactory tells Retrofit how to turn JSON into Kotlin objects.
val retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()val retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com/")
.addConverterFactory(MoshiConverterFactory.create())
.build()This simple Kotlin program creates a Retrofit instance with a base URL and Gson converter. It then prints the base URL to confirm setup.
import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory fun createRetrofit(): Retrofit { return Retrofit.Builder() .baseUrl("https://jsonplaceholder.typicode.com/") .addConverterFactory(GsonConverterFactory.create()) .build() } fun main() { val retrofit = createRetrofit() println("Retrofit instance created with base URL: ${retrofit.baseUrl()}") }
Always use a trailing slash / at the end of your base URL.
Make sure to add the Retrofit and converter dependencies in your build.gradle file.
Retrofit setup is the first step before making network calls.
Retrofit simplifies network calls by converting web data to Kotlin objects.
Setup requires a base URL and a converter like Gson.
Once set up, you can easily create services to fetch or send data.
Practice
Solution
Step 1: Understand Retrofit's role
Retrofit is a library designed to make network calls easier and convert JSON or XML responses into Kotlin objects automatically.Step 2: Compare with other options
Options A, B, and D relate to UI, databases, and navigation, which are not Retrofit's purpose.Final Answer:
To simplify network calls and convert web data into Kotlin objects -> Option BQuick Check:
Retrofit = Network calls + data conversion [OK]
- Confusing Retrofit with UI or database libraries
- Thinking Retrofit manages navigation
- Assuming Retrofit is for local storage
Solution
Step 1: Recall Retrofit Builder syntax
The correct way is to use Retrofit.Builder(), set the baseUrl, and then call build() to create the instance.Step 2: Check each option
val retrofit = Retrofit.Builder().baseUrl("https://api.example.com").build() uses the correct Builder pattern. Options A, B, and C use incorrect methods or order.Final Answer:
val retrofit = Retrofit.Builder().baseUrl("https://api.example.com").build() -> Option CQuick Check:
Builder + baseUrl + build() = correct setup [OK]
- Calling create() before build()
- Using newInstance() which doesn't exist
- Passing URL to create() instead of baseUrl()
service.getUsers() and the server returns JSON data?val retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
interface ApiService {
@GET("users")
suspend fun getUsers(): List<User>
}
val service = retrofit.create(ApiService::class.java)Solution
Step 1: Understand the converter role
GsonConverterFactory converts JSON responses into Kotlin objects automatically.Step 2: Analyze the service call
Calling getUsers() returns a List<User> parsed from JSON because of the converter and suspend function.Final Answer:
A List of User objects parsed from the JSON response -> Option DQuick Check:
GsonConverterFactory + suspend fun = parsed objects [OK]
- Expecting raw JSON instead of parsed objects
- Forgetting to add converter factory
- Not understanding suspend functions
val retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com")
.addConverterFactory(GsonConverterFactory)
.build()Solution
Step 1: Check addConverterFactory usage
GsonConverterFactory is a class; it requires calling create() method or parentheses to instantiate.Step 2: Verify other parts
baseUrl can have or not have trailing slash; Retrofit.Builder() is correct; build() must be last.Final Answer:
Missing parentheses after GsonConverterFactory -> Option AQuick Check:
addConverterFactory needs instance, not class [OK]
- Passing class instead of instance to addConverterFactory
- Confusing baseUrl format
- Wrong order of builder methods
Solution
Step 1: Understand converter order importance
Retrofit tries converters in order added. XML converter should come first to handle XML, then JSON.Step 2: Check method chaining and instantiation
Converters must be instantiated with create(). Retrofit.Builder().baseUrl("https://api.example.com/").addConverterFactory(SimpleXmlConverterFactory.create()).addConverterFactory(GsonConverterFactory.create()).build() adds XML first, then JSON, correctly.Step 3: Eliminate wrong options
Retrofit.Builder().baseUrl("https://api.example.com/").addConverterFactory(GsonConverterFactory.create()).addConverterFactory(SimpleXmlConverterFactory.create()).build() reverses order; C misses create() calls; D calls addConverterFactory after build(), which is invalid.Final Answer:
Retrofit.Builder().baseUrl("https://api.example.com/").addConverterFactory(SimpleXmlConverterFactory.create()).addConverterFactory(GsonConverterFactory.create()).build() -> Option AQuick Check:
Converters order matters; XML first, then JSON [OK]
- Wrong order of converters causing parsing errors
- Not calling create() on converters
- Adding converters after build() call
Ready to prove it?
Pass the test and this pattern moves unknown
