解析登录的网站从jsoup的WebView [英] Parsing logged-in website from webview with jsoup

查看:356
本文介绍了解析登录的网站从jsoup的WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了JavaScript和用户提示(外网站对话框,网页流量负载之前),我用它来登录该网站的网页视图的应用程序。现在,我想用jsoup一些信息得到的,但问题是,jsoup获取信息来自非登录主页。例如,如果我没有在网站登录网页并没有像信息的用户名,邮件等,我想用jsoup这些信息得到的,但我不能,因为jsoup是不实际读取同一页面,因为它是在web视图。有什么办法,我可以以某种方式连接jsoup和web视图等等jsoup阅读的网页,因为它们里面的WebView?我需要这使新讯息Android通知...

I've made an WebView application with javascript and user-prompt (dialog outside website, before webview loads) which I use to log in that website. Now I want to get some informations using jsoup, but problem is that jsoup gets informations from non-logged in homepage. For example, if I'm not logged in at website homepage does not have informations like Username, Messages etc. I want to get these informations using jsoup, but I can't since jsoup isn't actually reading same page as it's in webview. Is there any way I can somehow "connect" jsoup and webview so jsoup read pages as they are inside WebView? I need this to make android notifications about new messages...

在此先感谢!

推荐答案

您可以执行JavaScript来传递加载回包含它通过注册 JavascriptInterface

You can execute javascript to pass the HTML content that was loaded back to the Activity that contains it by registering a JavascriptInterface

您可以创建在活动界面为私有内部类:

You can create the interface in your Activity as a private inner class:

private class MyJavaScriptInterface {
    @JavascriptInterface
    public void handleHtml(String html) {
         // Use jsoup on this String here to search for your content.
         Document doc = Jsoup.parse(html);

         // Now you can, for example, retrieve a div with id="username" here
         Element usernameDiv = doc.select("#username").first();
    }
}

然后,附加给你的web视图:

Then, attach this to your WebView:

webview.addJavascriptInterface(new MyJavaScriptInterface(), "HtmlHandler");

最后,添加一个 WebViewClient 将每一个页面加载完成时调用此JavaScript方法。您必须确定何时基于网页的当时的内容用户登录:

Finally, add a WebViewClient that will call this javascript method every time the page loading completes. You will have to determine when the user is logged in based on the content of the page at that time:

webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            webview.loadUrl("javascript:window.HtmlHandler.handleHtml" +
                    "('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
        }
    });

这篇关于解析登录的网站从jsoup的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆