简体   繁体   English

如何将 html 代码从 webview 提取到 textview?

[英]How to extract the html code from webview to textview?

Its been a week since I start searching this question on the internet, But I cant get the solution.我开始在互联网上搜索这个问题已经一周了,但我找不到解决方案。

I want to get the html code from webview and put it in a textview on android.我想从 webview 获取 html 代码并将其放在 android 上的 textview 中。 I cant use jsoup connect because... I cant explain it too much, this is my example(example only)我无法使用 jsoup connect 因为...我无法解释太多,这是我的示例(仅示例)

On webview I have https://facebook.com(already login my fb account)在 webview 上我有 https://facebook.com(已经登录我的 fb 帐户)

I cant use jsoup connect https://facebook.com because the html is On the fb login activity, I mean not login from facebook我不能使用 jsoup 连接https://facebook.com因为 html 在 fb 登录活动中,我的意思是不要从 facebook 登录

I hope you understand我希望你明白

Create a load listener创建负载侦听器

class HTMLListener{
    public void processHTML(String html)
    {
        Log.e("output_html",html);
        // Then you can set your textview with html
    }
}

then in your webview config add this然后在你的 webview 配置中添加这个

webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new HTMLListener(), "LOADHTML");

then in webview client;然后在 webview 客户端;

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
 
                return true;
        }              
       
        @Override
        public void onPageStarted(WebView view, String url,
                        Bitmap favicon) {
        }
       
        public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:window.LOADHTML.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
        }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM