im没什么问题(一定很容易,因为我前段时间解决了myslef,但现在我又碰到它了,而我只是没看到它: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
在调用Form
时,JSON响应已转换为GsonConverterFactory
(可能是onResponse
)。
[您看到Form.toString()
调用的结果,如果要更改它,则需要在那里覆盖toString()
。