Skip to content Skip to sidebar Skip to footer

Parsing Logged-in Website From Webview With Jsoup

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 informati

Solution 1:

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:

privateclassMyJavaScriptInterface {
    @JavascriptInterfacepublicvoidhandleHtml(String html) {
         // Use jsoup on this String here to search for your content.Documentdoc= Jsoup.parse(html);

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

Then, attach this to your WebView:

webview.addJavascriptInterface(newMyJavaScriptInterface(), "HtmlHandler");

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(newWebViewClient() {
        @OverridepublicvoidonPageFinished(WebView view, String url) {
            webview.loadUrl("javascript:window.HtmlHandler.handleHtml" +
                    "('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
        }
    });

Post a Comment for "Parsing Logged-in Website From Webview With Jsoup"