我正在为短信备份创建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文件时所看到的那样,但仍然遇到此错误。
请帮助我解决此问题
答案 0 :(得分:1)
我怀疑您收到的是Unicode空字符,这就是它损坏的原因。
非法字符(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)
}
}
答案 1 :(得分:0)
"U+..."
表示它是utf16
符号。我猜它在带有表情符号的SMS上崩溃了,因为表情符号是utf16
符号。尝试使用表情符号保存SMS以检查此猜测。
xmlSerializer.text("")