Apix_D
Apix_D

Reputation: 1101

Unescape html string via kotlin

i have the problem that i can't unescape an escaped html file via kotlin.

I receive an escaped html string with a lot of content and i need to read the unescaped html format of that to display it in a webview.

This is a cuttout of my html...

enter image description here

I can read the .html file from my raw folder with this function..

private fun getTermsString(): String {
    Log.e("Fct", "Start Fct")
    val ist: InputStreamReader

    try {
        Log.e("Reader", "Start reader")
        //R.raw.x - x equals the html file
        ist = InputStreamReader(resources.openRawResource(R.raw.api_response))
        val theString = IOUtils.toString(ist)
        Log.e("Reader", "Reader finished")
        ist.close()
        return theString
    } catch (e: IOException) {
        System.err.println(e.printStackTrace())
    }
    return "Func did not load anything."
}

fun callVM() {
     try {
        url = getTermsString()
        var decodedHtml = unescape(url)
       
    wv_map.loadData(Base64.encodeToString(decodedHtml.toByteArray(StandardCharsets.UTF_8), Base64.DEFAULT),
                "text/html; charset=utf-8", "base64")

    } catch (e: Exception) {
        Log.e("FAIL", "Filedownload failed!")
        e.printStackTrace()
    }
}

For transformation i use

fun unescape(str: String) : String
{

    Log.e("decode", str)
    str.replace("\\s+", " ")
    str.replace("\\n","")
    str.replace("\\u003d","=")
    str.replace("\\u003c","<")
    str.replace("\\u003e",">")
    str.replace("\\u0027","'")
    str.replace("\\","\"")
    str.replace("\\t", "    ")
    return str
}

The StringEscapeUtils.unescapeHtml4() does not work for me, thats why I need a manual written function.

Does anybody has the same issue and got a solution for that?

Thanks for your help!

Upvotes: 0

Views: 2912

Answers (2)

Apix_D
Apix_D

Reputation: 1101

Thanks to all of you, but the Solution was... Well I was pretty silly :p

i just had to save my replace function in my variable like...

 fun unescape(str: String) : String
{
    var strVar = str
    Log.e("decode", strVar)
    strVar = strVar.replace("\\s+", " ")
...

this works fine. But thanks for your help! :)

Upvotes: 0

Simon
Simon

Reputation: 1737

You need to convert everything to byte array using UTF8 encoder and then convert it back to string again like this:

val utf8byteArray = "\u003d".toByteArray(Charsets.UTF_8)
val stringData = utf8byteArray.toString(Charsets.UTF_8)
println(stringData) // prints '='

In your case just replace "\u003d" with your entire html source.

Upvotes: 2

Related Questions