Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve and modify HTML content from WebView with Http Post

I'm developing an android app that shows some part of a HTML page. For the first step, I use this code provided in question How to retrieve HTML content from WebView.

  • Full html picture

part 1

  • That part i want to show

    part 2

    /* An instance of this class will be registered as a JavaScript interface */
    class MyJavaScriptInterface
    {
        @SuppressWarnings("unused")
        public void processHTML(String html)
        {
            // process the html as needed by the app
        }
    }
    
    final WebView browser = (WebView)findViewById(R.id.browser);
    /* JavaScript must be enabled if you want it to work, obviously */
    browser.getSettings().setJavaScriptEnabled(true);
    
    /* Register a new JavaScript interface called HTMLOUT */
    browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
    
    /* WebViewClient must be set BEFORE calling loadUrl! */
    browser.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url)
        {
            /* This call inject JavaScript into the page which just finished loading. */
            browser.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
        }
    });
    
    /* load a web page */
    browser.loadUrl("http://example.com/gethtml.html"); 
    

but when a user click on a button in second picture a HTTP post method will be called and this will be the result HTML
enter image description here

my question is how retrieve and modify the Post Result Html before showing to user? and show something like this enter image description here

like image 504
Mohammad Olfatmiri Avatar asked Aug 25 '15 17:08

Mohammad Olfatmiri


2 Answers

A simple solution could be to inject custom css code inside your webview, changing the style of the HTML element you want to display "alone".

You should inject a css like this:

#central-box {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    // depends on the structure of the page
}

See also in this answer for an example of how to do it.

like image 97
lifeisfoo Avatar answered Sep 19 '22 17:09

lifeisfoo


There are at least three ways to do this that I can think of and you're really close to all of them:

  1. You already have a rough idea of the quick and dirty method, from @lifeisfoo, although to handle both GET and POST pre-API 21 you would need to use onPageCommitVisible(WebView view, String url). As onPageFinished(WebView view, String url) and shouldOverrideUrlLoading (WebView view, String url) only trigger on GET requests.

Here's why, straight from the SDK... ..."onPageCommitVisible callback can be used to determine the point at which it is safe to make a recycled WebView visible, ensuring that no stale content is shown. It is called at the earliest point at which it can be guaranteed that onDraw(Canvas) will no longer draw any content from previous navigations. The next draw will display either the background color of the WebView, or some of the contents of the newly loaded page." it's called on all content updates POST or GET.

  1. Slightly better, you can create a local proxy web page with the javascript interface and other fun bits already in place and stored as a resource, if the content you want is loaded into full-window iframe you can get really good control, you can interact with your proxy using onPageFinished since it's in an iframe you can interact with the target page using onLoadResource, handle interception using the old shouldInterceptRequest which returns a WebResourceResponse and deal with POST driven updates using onFormResubmission.

  2. Finally, If API support earlier than 21 isn't a big issue the best method would be to use the new shouldInterceptRequest which takes the new WebResourceRequest object that allows you to specify different handling for different request types.

Here's the API Links for the new WebResourceRequest object and the new shouldInterceptRequest method on the WebViewClient...

http://developer.android.com/reference/android/webkit/WebResourceRequest.html

http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldInterceptRequest(android.webkit.WebView, android.webkit.WebResourceRequest)

hope that helps :)

like image 26
J-Boss Avatar answered Sep 18 '22 17:09

J-Boss