call.enqueue onResponse returns 空

call.enqueue onResponse returns null

我没有什么问题(一定很容易,因为我前段时间自己解决了它,但现在我又碰到了它,我只是没有看到它:P)。我只是没有从我的 api 电话中获得适当的价值。这是我的代码示例。

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)
        }
    }
}

我在这里 "Log.v("retfroit", data.value.toString())" 接收和显示的响应就像 "com.example.dynamicforms.data.entity.Form@5279360" 我希望看到 JSON 类似的响应。

提前致谢:D

在调用 onResponse 时,JSON 响应已经转换为 Form(可能是 GsonConverterFactory)。

您会看到 Form.toString() 调用的结果,如果您想更改它,您需要在那里覆盖 toString()