Home » Android » java – How to retrieve and modify HTML content from WebView with Http Post

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

Posted by: admin May 14, 2020 Leave a comment


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
        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 */
    /* Register a new JavaScript interface called HTMLOUT */
    browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
    /* WebViewClient must be set BEFORE calling loadUrl! */
    browser.setWebViewClient(new WebViewClient() {
        public void onPageFinished(WebView view, String url)
            /* This call inject JavaScript into the page which just finished loading. */
    /* load a web page */

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

How to&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.


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/WebViewClient.html#shouldInterceptRequest(android.webkit.WebView, android.webkit.WebResourceRequest)

hope that helps 🙂