-1

I tried to use this function:

URL("url").readText()

for example with the URL http://date.jsontest.com/ to get a JSON string and manipulate it. However it just doesn't work, it crash.

I mean how to have the text of what is display using kotlin on android ?

Ok I find a way

    val url: URL? = try {
    URL("https://www.instagram.com/p/CNVUZyrDVSQ/?__a=1")
    }catch (e: MalformedURLException){
    Log.d("Exception", e.toString())
    null
    }

and

fun URL.getString(): String? {
    val stream = openStream()
    return try {
        val r = BufferedReader(InputStreamReader(stream))
        val result = StringBuilder()
        var line: String?
        while (r.readLine().also { line = it } != null) {
            result.append(line).appendLine()
        }
        result.toString()
    }catch (e: IOException){
        e.toString()
    }
}

1 Answers1

-2

It's pretty similar:

val htmlString = File("/path/to/sample.html").readText()
Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42