Android – type mismatch: inferred type is string, but charset is expected in kotlin

My main activities have the following codes:

var qNa_list = parseQuestions(loadJSONFromAsset("qna_list.json"))


fun loadJSONFromAsset(file_name:String): String? {
    var json: String? = null
    try {

        val isis = assets.open(file_name)

        val size = isis.available()

        val buffer = ByteArray(size)

        isis.read(buffer)

        isis.close()

        json = String(buffer, "UTF-8")


    } catch (ex: IOException) {
        ex.printStackTrace()
        return null
    }

    return json

}

When I try to compile it, I get the following error

I fixed some other errors caused by nullables, but this is something I can't decode

Error: (127,35) type mismatch: inferred type is string but expected charset

I have changed some values to be nullable to accommodate errors, but JSON = string (buffer, "UTF-8") (UTF-8) is always underlined in red

resolvent:

This seems to solve the problem

It seems that I need to specify charset type objects instead of strings like UTF - 8

@Maro šŠ The first method mentioned by Eleng

Charset.forName("UTF-8")

Alternatively, specify charset.utf_ eight

val charset: Charset = Charsets.UTF_8

json = String(buffer, charset)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>