我正在尝试从 url 获取 html 并操作 html,然后使用 GeckoView 呈现它
我打算使用 Jsoup 从 url 获取 html 字符串,但这表示使用 Jsoup 获取完整的 html 存在问题
所以我找到了另一种使用 GeckoView 获取 html 的方法(这个和api 参考)
所以我成功地从一个 url 获取了一个 html 字符串并只用 GeckoView 渲染它
这是代码
private fun setupGeckoView() { // I call this in the onCreate method
geckoView = findViewById(R.id.geckoview)
val runtime = GeckoRuntime.create(this)
geckoSession.open(runtime)
geckoView.setSession(geckoSession)
val executor = GeckoWebExecutor(runtime)
val result = executor.fetch( // from here i start getting the sourcecode inputStream
WebRequest.Builder("https://google.com")
.header("Accept","Application/json")
.build())
result.then { response ->
htmlContentInStringFormat = response?.body?.bufferedReader().use { it?.readText() }
// ㄴ and i get the html string from the inputStream
geckoSession.loadData(htmlContentInStringFormat!!.toByteArray(Charsets.UTF_8),"text/html")
// ㄴ and then here i render and print the html string to my screen
result
}
urlEditText.setText(INITIAL_URL) // from here is just for setting a progressbar for web pages loading
progressView = findViewById(R.id.page_progress)
geckoSession.progressDelegate = createProgressDelegate()
}
问题是网页没有像下面这样完全打印出来
我对上面的代码还有一个问题
如果我在下面编写这样的代码,我发现了这一点
result.then { response ->
htmlContentInStringFormat = response?.body?.bufferedReader().use { it?.readText() }
result
}
geckoSession.loadData(htmlContentInStringFormat!!.toByteArray(Charsets.UTF_8),"text/html")
geckoSession.loadData() 方法比结果先执行。然后 lambda 函数
我不明白为什么会这样
所以我的问题是
- 为什么我不能完全打印网页?
- 为什么那个 lambda 函数执行得这么晚?