【发布时间】:2018-04-18 15:11:29
【问题描述】:
如果类类型是泛型类型'T',如何使用Gson进行解析?
拥有一个泛型类型的类,并且在其函数中将 json 字符串解析为运行时泛型类型“T”的任何内容。但是得到了
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example.model.content
这不适用于泛型是 'T':
val dataType = object : TypeToken<T>() {}.type
val data = Gson().fromJson<T>(jsonStr, dataType)
这就是使用 'solid' 类类型 Content 为传入的 Handler<Content> 调用调用的方式,其中需要通用 'T' (IHandler<T>)
FetchRemoteDataCommand("https", “testcontent.com", "v2/content”,
params,
Handler<Content>).execute()
这是一个采用泛型类型的类:IHandler<T>,期望在运行时“T”应该是“solid”类型,即它应该是调用时的 Content 类型
class FetchRemoteDataCommand<T>(scheme: String, authority: String, path: String,
params: HashMap<String, String>,
dataReadyCb: IHandler<T>) {
val mBaseUrl = "$scheme://$authority/"
val mPath = path
val mParams = params
var mDataReadyListener = dataReadyCb
override fun dispose() {……}
override fun execute() {
getRemoteData(object : Observer<ResponseBody> {
override fun onNext(responseBody: ResponseBody) {
val dataType = object : TypeToken<T>() {}.type
val jsonStr = responseBody.string()
val data = Gson().fromJson<T>(jsonStr, dataType) ***//<=== throws***
///
mDataReadyListener(data) //<== suppose to pass back the pared data back
……
}
…………
})
}
private fun getRemoteData(observer: Observer<ResponseBody>) : Unit {
val service: IRemoteRepositoryService = ServiceFactory.createRetrofitService(
IRemoteRepositoryService::class.java, mBaseUrl)
service.getRemoteData(mPath, mParams)
.subscribe(observer)
}
}
【问题讨论】: