Kotlin call.enqueue onResponse返回null

Kotlin call.enqueue onResponse返回null,kotlin,retrofit,Kotlin,Retrofit,我有一个小问题(一定很容易,因为我以前自己解决了,但现在我又碰到了,我只是没有看到它:p)。我只是没有从api调用中获得正确的值。这是我的代码示例 测试活性 fun fetchData() { var data = MutableLiveData<Form>() val call: Call<Form> = FormService.invoke().getFormById(40) call.enqueue(object : retrofit2.

我有一个小问题(一定很容易,因为我以前自己解决了,但现在我又碰到了,我只是没有看到它:p)。我只是没有从api调用中获得正确的值。这是我的代码示例

测试活性

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())
         }
    })
fun fetchData(){
var data=MutableLiveData()
val call:call=FormService.invoke().getFormById(40)
call.enqueue(对象:2.Callback{
覆盖失效时的乐趣(调用:调用,t:可丢弃){
Log.v(“改装”、“呼叫失败”)
}
覆盖fun onResponse(调用:调用,响应:2.响应){
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)
        }
    }
}
接口FormService{
@获取(“发送表单/表单”)
fun getFormById(@Query(“formId”)id:Int):调用
伴星{
val okHttpClient=okHttpClient.Builder()
.connectTimeout(1,时间单位为分钟)
.readTimeout(30,时间单位为秒)
.writeTimeout(15,时间单位。秒)
.build()
运算符fun invoke():FormService{
return reformation.Builder()
.客户(okHttpClient)
.baseUrl(URL)
.addCallAdapterFactory(coroutineCalAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(FormService::class.java)
}
}
}
我在这里接收并显示的响应“Log.v”(“retfroit”,data.value.toString())”类似于“com.example.dynamicforms.data.entity”。Form@5279360“当我期待看到类似JSON的响应时


提前感谢:在调用
onResponse
时,JSON响应已经转换为
格式(可能由
GsonConverterFactory


您可以看到
Form.toString()
调用的结果,如果您想更改它,您需要在那里重写
toString()

调用
onResponse
时,JSON响应已经转换为
表单(可能由
GsonConverterFactory


您可以看到
Form.toString()调用的结果,如果您想更改它,需要重写
toString()
在那里。

好的,原来这只是更大问题的一部分,那就是我的本地数据库,但你的回答也很有帮助,让我去了别处:)谢谢!好的,原来这只是更大问题的一部分,那就是我的本地数据库,但你的回答也很有帮助,让我去了别处:)谢谢!