gpt4 book ai didi

android - java.lang.IllegalArgumentException : Illegal character (U+0) in writing xml file android 异常

转载 作者:行者123 更新时间:2023-11-29 00:54:47 25 4
gpt4 key购买 nike

我正在为短信备份创建 xml 文件。它在大多数设备上工作正常,但在某些设备上,它会出错。

下面是编写xml文件的代码:

fun writeAllMsgs(msgList: ArrayList<MsgModel>, fileNm: String): Boolean{
val xmlSerializer = Xml.newSerializer()
val writer = StringWriter()
try {

xmlSerializer.setOutput(writer)
xmlSerializer.startDocument("UTF-8", true)
xmlSerializer.startTag("", MSG_ALL_SMS)

for (msg in msgList) {
xmlSerializer.startTag("", MSG_SMS)

xmlSerializer.startTag("", MSG_ADDRESS)
xmlSerializer.text(msg.address)
xmlSerializer.endTag("", MSG_ADDRESS)

xmlSerializer.startTag("", MSG_BODY)
xmlSerializer.text(msg.body)
xmlSerializer.endTag("", MSG_BODY)


xmlSerializer.startTag("", MSG_DATE)
xmlSerializer.text(msg.date)
xmlSerializer.endTag("", MSG_DATE)

xmlSerializer.startTag("", MSG_TYPE)
xmlSerializer.text(msg.type)
xmlSerializer.endTag("", MSG_TYPE)

xmlSerializer.startTag("", MSG_NAME)
xmlSerializer.text(msg.name)
xmlSerializer.endTag("", MSG_NAME)

xmlSerializer.startTag("", MSG_READ)
xmlSerializer.text(msg.read)
xmlSerializer.endTag("", MSG_READ)

xmlSerializer.startTag("", MSG_SER_CENTER)
xmlSerializer.text(msg.serviceCenter)
xmlSerializer.endTag("", MSG_SER_CENTER)

xmlSerializer.startTag("", MSG_PHOTO_URI)
if (msg.photo.isNullOrEmpty())
xmlSerializer.text("")
else
xmlSerializer.text(msg.photo)
xmlSerializer.endTag("", MSG_PHOTO_URI)
xmlSerializer.endTag("", MSG_SMS)

}

xmlSerializer.endTag("", MSG_ALL_SMS)
xmlSerializer.endDocument()
xmlSerializer.flush()

val createdFile = CreateSMSFile(fileNm)
val out = mContext.contentResolver.openOutputStream(createdFile)
val strData = writer.toString()
out!!.write(strData.toByteArray())
out.close()
return true

} catch (e: Exception) {
Applog.e(TAG, e)
return false
}
}

我收到以下崩溃日志:

java.lang.IllegalArgumentException: Illegal character (U+0)
at org.kxml2.io.KXmlSerializer.reportInvalidCharacter(KXmlSerializer.java:148)
at org.kxml2.io.KXmlSerializer.writeEscaped(KXmlSerializer.java:139)
at org.kxml2.io.KXmlSerializer.text(KXmlSerializer.java:540)
at com.allbackup.helpers.MsgHelper.writeAllMsgs(MsgHelper.java:156)
at com.allbackup.ui.activity.InnerHomeActivity$backupData.doInBackground(InnerHomeActivity.java:553)
at com.allbackup.ui.activity.InnerHomeActivity$backupData.doInBackground(InnerHomeActivity.java:523)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

根据上面的错误,它在这一行显示错误:

xmlSerializer.text(msg.body)

据我了解,它是由于拉丁字符或特殊字符而发生的,对于这种处理,我已经实现了“UTF-8”,正如您在编写 xml 文件时看到的那样,但仍然面临此错误。

请帮我解决这个问题

最佳答案

我怀疑您收到了一个 unicode 空字符,这就是它被破坏的原因。

Illegal character (U+0)

更多信息请访问:https://www.fileformat.info/info/unicode/char/0000/index.htm

记录 msg.body 的内容以确认这一点,如果是这种情况,您需要在尝试保存之前对其进行清理。

我能够使用此代码段重新创建原始错误消息

private fun writeMessageBodyTest() {

val xmlSerializer = Xml.newSerializer()

val writer = StringWriter()

try {

xmlSerializer.setOutput(writer)
xmlSerializer.startDocument("UTF-8", true)

val illegalChar = '\u0000'

xmlSerializer.startTag("", "message")
xmlSerializer.text("$illegalChar")
xmlSerializer.endTag("", "message")

xmlSerializer.endDocument()
xmlSerializer.flush()

Log.d(TAG, "Xml: ${writer.toString()}")

} catch (e: Exception) {
Log.e(TAG, e.message, e)
}
}

关于android - java.lang.IllegalArgumentException : Illegal character (U+0) in writing xml file android 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55608267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com