简体   繁体   中英

Android WebView extract HTML

Basically, I am developing an Android Application where the user can access the HTML Content of the Page and choose what to do with it next whether it be write to the File System , Upload Via FTP etc.. the user cannot see the HTML but it stores it for viewing later.

I have tried adding a JavaScript interface then adding it to the onPageFinished Event, it just doesnt want to hand over the HTML, heres what I did:

so for my onPageFinished event:

Webview wv = (WebView) findViewById(R.id.webbrowser);
wv.addJavascriptInterface(new JSEngine() , "JSEngine");
  public void onPageFinished(WebView wv , String html){
   //code to execute
   wv.loadUrl("javascript: window.JSEngine.processHTML(document.body.innerHTML)");
  }

the code to execute inside the processHTML cant be shared for certain reasons but i can tell you that the html variable is empty.

then my javascript interface:

Class JSEngine{
  @JavascriptInterface
  public void processHTML(String html){
    //code to execute
    //html variable is null
  }
}

Is there anything that can tackle my problem? All of my permissions are set, my class is working perfectly until it comes to The webview handing over the html.

If the user doesn't need to see the HTML, then forget about WebView , open the page with a regular HTTP stream, read the HTML data and work with that.

I suspect that there is actually is a preview of the web page, in which case your best bet is to read the HTML data stream, then turn around and display in in the WebView with loadDataWithBaseURL() .

Another possibility might be to override WebViewClient.onLoadResource() and open an HTTP stream to read the data on that URL. This is redundant, since the WebView is going to read the data itself, but the problem is that WebView just doesn't give you hooks into the HTTP data stream it is reading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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