I am trying to learn how to make REST calls from Kotlin using Retrofit. I have followed the example on Loopcupcakes.com which works and writes the result nicely to the logcat. That's all good and I (broadly) understand what it's doing. I'm now trying to adapt this to use my API, but my running-before-I-can-walk approach is giving me problems in the implementation.
My API works fine, and returns a result in postman
{
"APIResult": [
{
"id": 200,
"status_message": "Success",
"developer_message": "",
"user_message": "Success",
"suppliers": [
{
"supplier_name": "Supplier 1",
"tariff_name": "Variable 1",
"tariff_start_date": "2021-04-01"
},
{
"supplier_name": "Supplier 2",
"tariff_name": "Fixed",
"tariff_start_date": "2021-05-01"
}
]
}
]
}
The call in Kotlin doesn't return an error, but logcat displays D/TAG_: null
I have the gradle and the manifest right as the original code was working. This is my code:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val service = Retrofit.Builder()
.baseUrl("https://api.domain.com/")
.addConverterFactory(MoshiConverterFactory.create())
.build()
.create(CurrentSupplier::class.java)
service.getSupplier().enqueue(object : Callback<SupplierResponse> {
override fun onFailure(call: Call<SupplierResponse>, t: Throwable) {
Log.d("TAG_", "An error happened!")
t.printStackTrace()
}
override fun onResponse(call: Call<SupplierResponse>, response: Response<SupplierResponse>) {
/* This will print the response of the network call to the Logcat */
Log.d("TAG_", response.body().toString())
}
})
}
}
data class SupplierResponse(val results: List<Supplier>)
data class Supplier(val id: Int)//, val tariff_name: String)
interface CurrentSupplier {
@GET("/supplier?key=xxx&ipaddress=1.1.1.1&operation=current")
fun getSupplier(): Call<SupplierResponse>
}
So, my question is, how do I configure the interface and/or data classes to get to the supplier name and tariff name elements?