编写自定义Retrofit适配器的通用问题
在过去,我已经编写了基于此代码的自定义error handling适配器进行改进: https : //github.com/square/retrofit/blob/master/samples/src/main/java/com/example/retrofit/ErrorHandlingAdapter.java
哪些工作真的很好。
我现在正在Kotlin编写我们的新应用程序,当转换代码时,呼叫适配器工厂给我一个错误,特别是在get
函数中。 它转换成这样的样子:
override fun get(returnType: Type, annotations: Array, retrofit: Retrofit): CallAdapter? { if (CallAdapter.Factory.getRawType(returnType) != MyCall::class) { return null } if (returnType !is ParameterizedType) { throw IllegalStateException( "MyCall must have generic type (eg, MyCall)") } val responseType = CallAdapter.Factory.getParameterUpperBound(0, returnType) val callbackExecutor = retrofit.callbackExecutor() return ErrorHandlingCallAdapter(responseType, callbackExecutor). \\This line produces an error }
错误说Projections are not allowed on type arguments of functions and properties
我已经尝试了几种方法来提取返回types的参数,因为到最后我知道我正在处理MyCall
然而,我试过的每一个方法都不能给我一个T
的版本,我可以用它替换它。 这是令人沮丧的,因为只要types从调用适配器返回,T的types是相当不相关的。
我知道我可以将它作为一个Java类导入并以此方式使用,但是我的目标是尽可能在Kotlin中编写所有内容。 在Kotlin中可以做到这一点吗?
- 如何使用Kotlin设置OnEditorActionListener
- 如何在android中创建动态布局
- 是否有必要知道kotlin获得AAD(Associate Android Developer)认证?
- 从Java转换到Kotlin之后,Dagger的模块不工作
- 更简单的方法来获取视图的Id(字符串)的Id(int)
- 类未find,androidTest中使用Android Studio 3.0.1,Room,Kotlin的空测试套件
- 运行Kotlin代码时,Android Studio会抛出java.lang.ClassNotFoundExceptionexception
- Android Studio 3.0.1(错误:’java’插件已被应用,但与Android插件不兼容。)
- 以编程方式更改可绘制的颜色