2

I am trying to get the HTML content of a webview as a string in Kotlin. I need to get the HTML of what the webview is showing, not the HTML of the URL it's showing. That is because the user is supposed to log in on the website, and if I try to get the HTML from the URL I only get the HTML from the login page.

This is how I'm currently getting the HTML from a URL:

class ReadFileTask{

    fun getTextFromWeb(urlString: String) {
        Thread(Runnable {
            try {
                val url = URL(urlString)
                val con = url.openConnection() as HttpsURLConnection

                val datas = con.inputStream.bufferedReader().readText()
                Log.d("HTML content", datas)

            } catch (ex: Exception) {
                Log.d("Exception", ex.toString())
            }
        }).start()
    }
}

Else I haven't done anything to my WebView besides adding a WebViewClient that makes it open URLs inside the webview. It looks like this:

class WebViewController : WebViewClient() {
    override fun shouldOverrideUrlLoading(
        view: WebView,
        url: String
    ): Boolean {
        view.loadUrl(url)
        view.settings.javaScriptEnabled = true

        return true
    }
}
onofricamila
  • 930
  • 1
  • 11
  • 20

0 Answers0