Alky
Alky

Reputation: 67

call.enqueue onResponse returns null

im having little problem(must be easy cuz i solved it myslef some time ago but now i bumped on it again and im just not seeing it :P). Im just not getting proper value from my api call. Here is sample of my code.

TestActivity.kt

fun fetchData() {
    var data = MutableLiveData<Form>()

    val call: Call<Form> = FormService.invoke().getFormById(40)

    call.enqueue(object : retrofit2.Callback<Form> {
         override fun onFailure(call: Call<Form>, t: Throwable) {
              Log.v("retrofit", "call failed")
         }

         override fun onResponse(call: Call<Form>, response: retrofit2.Response<Form>) {
              data.value = response.body()!!
              Log.v("retfroit", data.value.toString())
         }
    })

ApiService.kt

interface FormService {

    @GET("sendform/form")
    fun getFormById(@Query("formId") id: Int): Call<Form>

    companion object{

        val okHttpClient = OkHttpClient.Builder()
            .connectTimeout(1, TimeUnit.MINUTES)
            .readTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(15, TimeUnit.SECONDS)
            .build()

        operator fun invoke() : FormService{
            return Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(URL)
                .addCallAdapterFactory(CoroutineCallAdapterFactory())
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(FormService::class.java)
        }
    }
}

The response that im receiving and diplaying here "Log.v("retfroit", data.value.toString())" is like "com.example.dynamicforms.data.entity.Form@5279360" when im expecting to see JSON alike response.

Thanks in advance :D

Upvotes: 0

Views: 243

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170919

The JSON response is already converted to a Form (by GsonConverterFactory, probably) by the time onResponse is called.

You see the result of a Form.toString() call, and if you want to change it, you need to override toString() there.

Upvotes: 2

Related Questions