【问题标题】:getting retrofit exception Method return type must not include a type variable or wildcard in kotlin获取改造异常方法返回类型不能在 kotlin 中包含类型变量或通配符
【发布时间】:2017-08-31 18:23:13
【问题描述】:
 networkCall = NetworkCall(context)
            val responceCall = networkCall!!.getRetrofit(true).callReadMeService()
            responceCall.clone().enqueue(object : Callback<BaseResponse<*>?> {
                override fun onResponse(call: Call<BaseResponse<*>?>, response: Response<BaseResponse<*>?>) {
                    networkCall!!.dismissLoading()


                    web_view!!.loadData((response.body()?.data as LinkedTreeMap<*, *>)["description"] as String, "text/html", "UTF-8")
                }

                override fun onFailure(call: Call<BaseResponse<*>?>, t: Throwable) {
                    networkCall!!.dismissLoading()

                }
            })

这里是api方法

@POST("stories/readme")
fun callReadMeService():   Call<BaseResponse<*>?>

现在我遇到了这个异常

这是我的 BaseResponse 类

class BaseResponse<T> {

    var message: String? = null
    var status: Boolean = false
    var errors: Array<String>? = null
    var code: String? = null
    var data: T? = null
}

方法返回类型不能包含类型变量或通配符:retrofit2.Call> 对于方法 IApi.callReadMeService

现在我无法从 BaseResponse 类中删除通用数据变量,因为我将此类用作通用 Api 解析器

任何解决方案

【问题讨论】:

  • 这个问题你解决了吗?我有同样的问题。

标签: kotlin kotlin-android-extensions


【解决方案1】:

您不能这样做,因为需要完全指定类型信息,否则改造无法正确生成服务。 See this discussion.

您需要为每种类型创建不同的 API 方法。

【讨论】:

  • 有什么注释可以解决这个问题吗?
  • 如果我想返回地图怎么办? Kotlin 强制我声明类型参数!
  • @HosseinNasr 您最好的选择可能是返回一个JsonElement 并将其解析为您自己的Map。这个限制与 Kotlin 无关,而与 Retrofit 本身有关。
猜你喜欢
  • 2019-08-22
  • 1970-01-01
  • 2017-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多