【发布时间】:2018-05-25 21:49:01
【问题描述】:
我正在创建一个自定义键盘应用程序。在onCreateInputView 方法中,我在协程中包含以下代码,该协程解析来自https://github.com/vedantroy/image-test/raw/master/index.json 的文件(使用Gson 库中的JsonParser)。
Log.d("VED-APP","Parsing File From Github")
JsonParser().parse(fetchString("https://github.com/vedantroy/image-test/raw/master/index.json"))
其中fetchString() 是以下方法:
private suspend fun fetchString(url: String): String = suspendCoroutine { cont ->
url.httpGet().header(Pair("pragma", "no-cache"), Pair("cache-control", "no-cache")).responseString { _, _, result ->
//Log.d("VED-APP", r.toString())
when (result) {
is Result.Failure -> {
cont.resumeWithException(result.getException())
}
is Result.Success -> {
cont.resume(result.value)
}
}
}
}
上面的代码运行良好,没有任何错误。我可以使用 Gson 成功解析位于 URL 的文件。
但是,当我尝试使用以下代码从本地源而不是 github 解析文件(具有相同的确切内容)时,出现异常。
val layoutFileName = "index.json"
val layoutFile = File(filesDir, layoutFileName)
//If layout file does not exist, create it and write default layout to file.
if(!layoutFile.exists()) {
Log.d("VED-APP","FILE DOES NOT EXIST")
layoutFile.writeText(resources.getString(R.string.default_keyboard_layout))
} else {
Log.d("VED-APP","FILE EXISTS")
}
Log.d("VED-APP","Parsing Local File")
JsonParser().parse(FileReader(layoutFile))
例外是:
com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:未终止的数组在第 1 行第 18 列路径 $.Excited1
这是strings.xml中的条目default_keyboard_layout的内容:
这很奇怪,因为strings.xml 中的default_keyboard_layout 条目的内容与github 上的index.json 文件的内容完全相同。
我做了一些调试,我想我找到了错误的根源。
我将此添加到我的代码中,以便在解析之前查看index.json 的内容
layoutFile.forEachLine {
Log.d("VED-APP",it)
}
我得到了以下结果:
{ Excited:[https://github.com/vedantroy/image-test/raw/master/Happy/excited1.gif, https://github.com/vedantroy/image-test/raw/master/Happy/excited2.gif, https://github.com/vedantroy/image-test/raw/master/Happy/excited3.gif], Sad:[https://github.com/vedantroy/image-test/raw/master/Sad/sad1.gif, https://github.com/vedantroy/image-test/raw/master/Sad/sad2.gif, https://github.com/vedantroy/image-test/raw/master/Sad/sad3.gif, https://github.com/vedantroy/image-test/raw/master/Sad/sad4.gif] }
这个输出有两点很奇怪。
1)虽然strings.xml中的条目和github上的文件都是多行的,但一切都在一行中
2) 尽管strings.xml中的条目和github上的文件有引号,但文件没有引号。
我怀疑这两个问题是问题的根源,但我不确定。
为什么会出现此异常以及如何解决?
更新 1 -
我尝试了 Tynn 的解决方案,也更改了我的 strings.xml
<resources>
<string name="app_name">Anime Face Keyboard</string>
<string name="subtype_en_US">English (US)</string>
<string name="default_keyboard_layout"><![CDATA[{
"Excited":["https://github.com/vedantroy/image-test/raw/master/Happy/excited1.gif",
"https://github.com/vedantroy/image-test/raw/master/Happy/excited2.gif",
"https://github.com/vedantroy/image-test/raw/master/Happy/excited3.gif"],
"Sad":["https://github.com/vedantroy/image-test/raw/master/Sad/sad1.gif",
"https://github.com/vedantroy/image-test/raw/master/Sad/sad2.gif",
"https://github.com/vedantroy/image-test/raw/master/Sad/sad3.gif",
"https://github.com/vedantroy/image-test/raw/master/Sad/sad4.gif"]
}]]></string>
</resources>
但这没有任何作用。
我打印了resources.getString(R.string.default_keyboard_layout) 的值,但它仍然缺少引号和换行符。
【问题讨论】: