• 14
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I am creating xml file for sms backup. It works fine in most of devices , but in some devices, it gives error.

Below is the code for writing xml file:

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
        }
    }

I am getting following crash log:

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)

As per above error, it shows error on this line:

xmlSerializer.text(msg.body)

As my understanding, it happens due to latin character or special character and for that handling I already implemented "UTF-8" as you can see in writing xml file but still facing this error.

Kindly help me with this issue to resolve

I suspect that you are receiving a unicode null character and that is the reason why it is breaking.

Illegal character (U+0)

More info can be found at: https://www.fileformat.info/info/unicode/char/0000/index.htm

Log the contents of msg.body to confirm this and if it is the case you need to sanitize it before you attempt to save it.

I was able to recreate the original error message using this snippet

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
Reply Report