2011-03-10 72 views
35

如何检索当前在WebView中显示的所有HTML内容?如何从WebView中检索HTML内容(作为字符串)

我发现WebView.loadData()但我无法找到相反的当量(如WebView.getData())

请注意,我感兴趣的检索的网页,我无法控制的数据(即我不能将Javascript函数注入到这些页面中,以便在WebView中调用Javascript接口)。

+0

可能重复[我怎样网页内容从WebView?](http://stackoverflow.com/questions/2376471/how-do-i-get-the-web-page-contents-from-a-webview) – Guru 2013-03-20 14:50:45

回答

16

不幸的是,有没有简单的方法来做到这一点。

How do I get the web page contents from a WebView?

你可以只让一个HTTPRequest,在同一页面的网页视图,并得到响应。

+0

谢谢,你的建议是肯定比我目前发现的要好,但是......使HttpRequest成为一个pa已经加载到WebView的ge会双倍带宽要求(加载每个浏览过的页面TWICE)。有更好的解决方案吗? – JohnK 2011-03-10 18:59:43

+2

您可以进行一次调用,使HttpRequest获取数据,然后使用WebView.LoadData将其推送到WebView中 – brendan 2011-03-10 19:16:18

+0

如果特定页面位于登录凭证的后面,该怎么办? – 2012-10-10 22:10:47

30

你可以做到这一点通过:

final Context myApp = this; 

/* 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://lexandera.com/files/jsexamples/gethtml.html"); 

您将获得processHTML方法的整个HTML contnet。 ,它不会再提出网页请求。所以它也是这样做的更有效的方法。

谢谢。

+3

看起来类似于http:// lexandera。COM/2009/01 /提取-HTML-从-A-web视图/。该教程最后还有一个警告。 – 2014-01-10 22:19:57

+1

@shridutt kothari http://stackoverflow.com/questions/28194699/webview-content-are-not-loaded-properly-if-it-is-xml – GOLDEE 2015-01-29 11:16:52

+1

如果被加载的内容不是HTML格式,即XML或其他任何东西现在javascriptinterface没有得到调用,并最终与TypeError – GOLDEE 2015-01-29 11:20:00

0

你可以通过webview中的JavaScriptInterface传递数据..我已经做到了。 将数据保存为静态变量,然后在Android应用程序中进行处理

+0

你不需要把它保存到一个静态变量,否则这种方法当然是正确的 - 就像它已经被上面的kothari所示。 – 2014-04-10 17:08:10

1

您可以拦截WebView所做的HTTP请求,然后修改HTML以包含您需要与HTML页面通信的任何JavaScript函数。您可以通过WebViewClient shouldInterceptRequest()方法拦截HTTP请求。

使用此机制,您可以通过自己加载来访问加载的页面,在将其传递到WebView之前对其进行修改,甚至可以将其缓存在本地(如果需要)。

8
webView.evaluateJavascript("(function(){return window.document.body.outerHTML})();", 
     new ValueCallback<String>() { 
      @Override 
      public void onReceiveValue(String html) { 

      } 
     }); 
+0

作品像一个魅力 – 2017-07-05 10:19:20

0

添加到您的代码:

private String getUrlSource(String site) throws IOException { 
    //GNU Public, from ZunoZap Web Browser 
    URL url = new URL(site); 
    URLConnection urlc = url.openConnection(); 
    BufferedReader in = new BufferedReader(new InputStreamReader(
    urlc.getInputStream(), "UTF-8")); 
    String inputLine; 
    StringBuilder a = new StringBuilder(); 
    while ((inputLine = in.readLine()) != null) 
    a.append(inputLine); 
    in.close(); 

    return a.toString(); 
} 

那么可以说你得到什么谷歌的来源,你会怎么做:

getURLSource("http://google.com"); 
+1

崩溃在缓冲器。 – 2016-07-24 20:27:29

+0

不应该崩溃下载它在那里工作的ZunoZap浏览器 – 2016-07-27 04:37:46

相关问题