在 Kotlin Android 中阅读 webview 内容

问题描述

我需要阅读 Webview 中显示的内容。我有一个案例,我需要调用 webview 来输入密码,成功时返回 json 响应。现在,如果在 webview 屏幕本身中打印,则 json 响应。尝试使用 webview 函数读取响应

override fun onPageFinished(view: WebView?,url: String?) {
  super.onPageFinished(view,url)
  try {
      val aURL = URL(url)
      val conn: URLConnection = aURL.openConnection()
      conn.connect()
      val inputStream: InputStream = conn.getInputStream()
      htmlContent = convertToString(inputStream)
  }
  catch (exception:Exception){
      print(exception)
  }
}

执行 conn.getInputStream() 时导致 File not found 异常。在 onPageCommitVisible 函数中也尝试了相同的方法。结果一样。

还尝试使用 implementation 'org.jsoup:jsoup:1.11.2' 读取响应。但它会抛出 404 错误。

 val stringBuilder = StringBuilder()
 try {
     val doc: Document = Jsoup.connect(url).get()
     print(doc)
 } catch (e: IOException) {
    print(e)
 }

页面加载完成后有没有办法读取页面内容。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)