Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap不能强制转换为com.readbook.chinesepoetry.data.model.Response?;_Java_Android_Kotlin - Fatal编程技术网

为什么java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap不能强制转换为com.readbook.chinesepoetry.data.model.Response?;

为什么java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap不能强制转换为com.readbook.chinesepoetry.data.model.Response?;,java,android,kotlin,Java,Android,Kotlin,我是kotlin Android开发者的初学者。我遇到了这个问题,我不知道如何解决它 我定义了一个kotlin扩展函数Observable.totTraditional(),以保持Rx流调用,但遇到了一个异常 这是我的密码: class Test { @Test fun test() { val json = "{\"result\":{\"curPage\":1,\"data\":[{\"id\":1,\"name\":\"关雎\",\"dynasty\":\"

我是kotlin Android开发者的初学者。我遇到了这个问题,我不知道如何解决它

我定义了一个kotlin扩展函数
Observable.totTraditional()
,以保持Rx流调用,但遇到了一个异常

这是我的密码:

class Test {
    @Test
    fun test() {
        val json = "{\"result\":{\"curPage\":1,\"data\":[{\"id\":1,\"name\":\"关雎\",\"dynasty\":\"先秦\",\"poet\":\"佚名\",\"content\":\"钟鼓乐之。\"},{\"id\":1213,\"name\":\"伐檀\",\"dynasty\":\"先秦\",\"poet\":\"佚名\",\"content\":\"钟鼓乐之。\"}],\"pageCount\":388,\"size\":20,\"total\":7756},\"status\":1,\"message\":\"成功\"}"
        Observable.create<Response<RecommendListBean>> {
            val response = getResponse<Response<RecommendListBean>>(json)
            it.onNext(response)
        }.toTraditional().subscribe({},{
            println(it)
        })
    }

    inline fun <reified T> getResponse(json: String): T {
        return fromJson(json)
    }

    inline fun <reified T> Observable<T>.toTraditional(): Observable<T> {
        return map {
            val toJson = Gson().toJson(it)
            // In fact, I convert simplified chinese in json to traditional chinese.
            // Here, I intentionally omit that code to make my problem more clearly.
            val result = fromJson<T>(toJson)
            result
        }
    }

    inline fun <reified T> fromJson(json: String?): T {
        return Gson().fromJson<T>(json, object : TypeToken<T>() {}.type)
    }
}
运行上述代码后,我得到错误:

java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap无法强制转换为com.readbook.chinesepoetry.data.model.Response

我在网上查过这个例外,很多人说是因为kotlin泛型删除了。但是,我检查了我的代码,它们确实有
具体化的
关键字。

有趣的可观察的。toTraditional():可观察的{
fun <R :Any> Observable<R>.toTraditional(): Observable<R> {
return map {
    val toJson = Gson().toJson(it)
    // In fact, I convert simplified chinese in json to traditional chinese.
    // Here, I intentionally omit that code to make my problem more clearly.
    val result = Gson().fromJson<R>(toJson,it.javaClass)
    result
  }
}
返回图{ val toJson=Gson().toJson(it) //事实上,我将json中的简体中文转换为繁体中文。 //在这里,我故意省略该代码,以便更清楚地说明我的问题。 val result=Gson().fromJson(toJson,it.javaClass) 后果 } }

仅获取对象的类

类型擦除仍在发生,即使使用
具体化
。您需要捕获类:
T::class.java
您能详细解释一下如何捕获类吗?
data class RecommendListBean(
        val curPage: Int,
        val data: List<PoetryBean>,
        val pageCount: Int,
        val size: Int,
        val total: Int
)
data class PoetryBean (
    val content: String,
    val dynasty: String,
    val id: Int,
    val name: String,
    val poet: String
)
fun <R :Any> Observable<R>.toTraditional(): Observable<R> {
return map {
    val toJson = Gson().toJson(it)
    // In fact, I convert simplified chinese in json to traditional chinese.
    // Here, I intentionally omit that code to make my problem more clearly.
    val result = Gson().fromJson<R>(toJson,it.javaClass)
    result
  }
}