简体   繁体   English

Android WebView提取HTML

[英]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. 基本上,我正在开发一个Android应用程序,用户可以访问页面的HTML内容,然后选择如何处理它,无论是写入文件系统,通过FTP上传等等。用户无法看到HTML但是它存储它以供以后查看。

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: 我尝试添加一个JavaScript接口,然后将其添加到onPageFinished事件,它只是不想交出HTML,继承我所做的:

so for my onPageFinished event: 所以对于我的onPageFinished事件:

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. 在processHTML中执行的代码由于某些原因无法共享,但我可以告诉你html变量是空的。

then my javascript interface: 那我的javascript界面​​:

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. 我的所有权限都已设置,我的课程正常运行,直到涉及到webview移交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. 如果用户不需要查看HTML,那么忘记WebView ,使用常规HTTP流打开页面,读取HTML数据并使用它。

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() . 我怀疑实际上有一个网页的预览,在这种情况下,最好的办法是读取HTML数据流,然后转向并在带有loadDataWithBaseURL()WebView显示。

Another possibility might be to override WebViewClient.onLoadResource() and open an HTTP stream to read the data on that URL. 另一种可能是覆盖WebViewClient.onLoadResource()并打开HTTP流以读取该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. 这是多余的,因为WebView将自己读取数据,但问题是WebView只是不会挂钩它正在读取的HTTP数据流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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