长路漫漫,bug作伴

retrofit:Unable to create call a

2020-09-02  本文已影响0人  慕尼黑凌晨四点

环境:使用了retrofit2+协程
原因:Retrofit中未找到Object类相对应的CallAdapter

解决方法:

1 看callAdapterFactory是否添加正确

2 接口方法不能用suspend

interface Api{
    @GET("auth")
    suspend fun getTokenAsync(): Deferred<Bean>
}

改为

interface Api{
    @GET("auth")
   fun getTokenAsync(): Deferred<Bean>
}

原理:retrofit源码中用到动态代理,返回值类型存放在动态代理的method方法中。加了suspend关键字后method.getReturnType恒为Object类型(再问为什么我也不知道了)

retrofit相关文章

你真的会用Retrofit2吗?Retrofit2完全教程
当Retrofit遇上协程
retrofit2-kotlin-coroutines-adapter

上一篇 下一篇

猜你喜欢

热点阅读