gpt4 book ai didi

generics - 用Kotlin retrofit 转换器:类型推断失败

转载 作者:行者123 更新时间:2023-12-02 12:53:46 25 4
gpt4 key购买 nike

我正在尝试用Kotlin写一个 retrofit 转换器。但是,在尝试返回转换器时,我卡住了:

class JSONRPCConverterFactory private constructor(private val gson : Gson): Converter.Factory() {

companion object {
private val MEDIA_TYPE = MediaType.get("application/json")

fun create(gson: Gson) = JSONRPCConverterFactory(gson)
}

inner class JSONRPCRequestBodyConverter<T : JSONRPCRequest<*>>(private val gson: Gson) : Converter<T, RequestBody> {

override fun convert(value: T): RequestBody? {
val jsonString = gson.toJson(value, object:TypeToken<T>(){}.type)
return RequestBody.create(MEDIA_TYPE, jsonString)
}

}

inner class JSONRPCResponseBodyConverter<T>(private val gson: Gson) : Converter<ResponseBody, T> {

override fun convert(value: ResponseBody): T? {
return gson.fromJson(value.string(), object:TypeToken<T>(){}.type)
}

}

override fun responseBodyConverter(type: Type, annotations: Array<Annotation>, retrofit: Retrofit): Converter<ResponseBody, *>? {
if (!hasJSONRPCAnnotation(annotations)) return null

return JSONRPCResponseBodyConverter(gson)
}

override fun requestBodyConverter(type: Type, parameterAnnotations: Array<Annotation>, methodAnnotations: Array<Annotation>, retrofit: Retrofit): Converter<*, RequestBody>? {
if (!hasJSONRPCAnnotation(methodAnnotations)) return null

return JSONRPCRequestBodyConverter(gson)
}

private fun hasJSONRPCAnnotation(annotations: Array<Annotation>) : Boolean {
for (annotation in annotations) {
if (annotation is JSONRPC) return true
}

return false
}
}

错误出现在这两行中:
return JSONRPCResponseBodyConverter(gson)


return JSONRPCRequestBodyConverter(gson)

Type inference failed: Not enough information to infer parameter T in

constructor JSONRPCRequestBodyConverter> ( gson: Gson )

Please specify it explicitly.



在Java中,可以仅返回 new JSONRPCResponseBodyConverter<>(gson)。但是,在kotlin中,该类型是必需的,因此仅添加 <>也会失败。

使用Kotlin-lang: Kotlin Serialization Converter对该转换器进行了研究,我发现它使用几乎相同的类结构,并且只是返回了一个没有菱形的新转换器,并且可以正常工作。

我想念什么?

最佳答案

问题是JSONRPCResponseBodyConverterJSONRPCRequestBodyConverter都需要一个类型参数T,并且编译器无法在调用站点上推断出此类型。您看到的示例可能具有某种推断T类型的方法,例如T的参数或它要赋值的类型。

class A<T>(t: T? = null)

val a1 : A<Int> = A() //this will work, it can infer the type from the asigment
val a2 = A(2) //this will work, it can infer the type from the parameter
val a3 = A() //this won't work, the compiler has no way of knowing the type of T

关于generics - 用Kotlin retrofit 转换器:类型推断失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53763220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com