Retrofit: an Interface Becomes a Client

Turning a contract into code you can call

You could build every request by hand β€” assemble a URL string, open a connection, parse the response. PawWalk uses a library called Retrofit instead, and it does something that looks almost like magic the first time you see it: you write an interface β€” a Kotlin type with function signatures but no bodies β€” annotate each function with an HTTP verb and path, and Retrofit generates a real, working network client from it at runtime.

Two annotations you'll use constantly:

  • @GET("walkers") on a function β€” "calling this function performs GET /walkers."
  • suspend fun β€” every network call in PawWalk is a suspend function. You met suspend in Module 3: it means "this pauses without freezing the app," which is exactly what a network round-trip needs.

Retrofit reads the return type too. Declare suspend fun getWalkers(): List<Walker> and Retrofit decodes the JSON array straight into a List<Walker> for you β€” no manual JSON parsing code anywhere in sight.