Android 是否可以从 WebView 获取 HTML 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3479833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to get the HTML code from WebView
提问by Aymon Fournier
I would like to preemptively get the HTML code of a webpage that is to be loaded in a webView
, parse it using regex, and display only the HTML code that I want, while letting the webpage still think it has loaded everything.
我想抢先获取webView
要在 .
Is there any way to do that in the WebViewClient.onLoadResource()
or similar methods?
有什么办法可以在WebViewClient.onLoadResource()
或类似的方法中做到这一点?
EDIT: I tried this:
编辑:我试过这个:
class MyJavaScriptInterface
{
@SuppressWarnings("unused")
public void showHTML(String html, Context context)
{
new AlertDialog.Builder(context)
.setTitle("HTML")
.setMessage(html)
.setPositiveButton(android.R.string.ok, null)
.setCancelable(false)
.create();
pageHTML = html;
}
}
@Override
public void customizeWebView(final ServiceCommunicableActivity activity, final WebView webview, final SearchResult mRom) {
mRom.setFileSize(getFileSize(mRom.getURLSuffix()));
webview.getSettings().setJavaScriptEnabled(true);
MyJavaScriptInterface interfaceA = new MyJavaScriptInterface();
webview.addJavascriptInterface(interfaceA, "HTMLOUT");
WebViewClient anchorWebViewClient = new WebViewClient()
{
@Override
public void onPageFinished(WebView view, String url)
{
/* This call inject JavaScript into the page which just finished loading. */
webview.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
Pattern pattern = Pattern.compile("<h2>Winning Sc.+</h2></div>(.+)<br>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(pageHTML);
matcher.find();
The interface is never called
接口永远不会被调用
采纳答案by Aymon Fournier
Had to use HttpClient. no cookies required, just parsing for html:
不得不使用HttpClient。不需要 cookie,只需解析 html:
private String getDownloadButtonOnly(String url){
HttpGet pageGet = new HttpGet(url);
ResponseHandler<String> handler = new ResponseHandler<String>() {
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
HttpEntity entity = response.getEntity();
String html;
if (entity != null) {
html = EntityUtils.toString(entity);
return html;
} else {
return null;
}
}
};
pageHTML = null;
try {
while (pageHTML==null){
pageHTML = client.execute(pageGet, handler);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Pattern pattern = Pattern.compile("<h2>Direct Down.+?</h2>(</div>)*(.+?)<.+?>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(pageHTML);
String displayHTML = null;
while(matcher.find()){
displayHTML = matcher.group();
}
return displayHTML;
}
@Override
public void customizeWebView(final ServiceCommunicableActivity activity, final WebView webview, final SearchResult mRom) {
mRom.setFileSize(getFileSize(mRom.getURLSuffix()));
webview.getSettings().setJavaScriptEnabled(true);
WebViewClient anchorWebViewClient = new WebViewClient()
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
String downloadButtonHTML = getDownloadButtonOnly(url);
if(downloadButtonHTML!=null && !url.equals(lastLoadedURL)){
lastLoadedURL = url;
webview.loadDataWithBaseURL(url, downloadButtonHTML, null, "utf-8", url);
}
}
回答by Pentium10
Here is a tutorial of Extracting HTML from a WebViewdon't forget to read the warning in the end of the tutorial.
回答by eldy
Try to add @JavascriptInterface before public void showHTML(String html, Context context)
尝试在 public void showHTML(String html, Context context) 之前添加 @JavascriptInterface
回答by mc.dev
In case you have a chance to influence server part where you receive a page from, you can ask to redirect to a particular page in case of error. In your WebViewClient you can detect this redirect and use it a signal of error.
如果您有机会影响接收页面的服务器部分,您可以要求重定向到特定页面以防出错。在您的 WebViewClient 中,您可以检测到此重定向并将其用作错误信号。