java.lang.IllegalArgumentException: 写入 xml 文件中的非法字符 (U+0) android
java.lang.IllegalArgumentException: Illegal character (U+0) in writing xml file android
我正在为短信备份创建 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.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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 文件中看到的那样,但仍然面临此错误。
请帮我解决这个问题
"U+..."
表示是utf16
符号。我猜它会在带有表情符号的 SMS 上崩溃,因为表情符号是 utf16
符号。尝试用表情符号保存短信来验证这个猜测。
xmlSerializer.text("")
我怀疑您收到的是 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)
}
}
我正在为短信备份创建 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.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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 文件中看到的那样,但仍然面临此错误。
请帮我解决这个问题
"U+..."
表示是utf16
符号。我猜它会在带有表情符号的 SMS 上崩溃,因为表情符号是 utf16
符号。尝试用表情符号保存短信来验证这个猜测。
xmlSerializer.text("")
我怀疑您收到的是 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)
}
}