我发现从webview读取内容的问题.我试过无法修复.
我已经在webview中显示了一个html页面.我需要字符串中该html页面的内容
WebView mainContent = (WebView)layout.findViewById(R.id.webView1);
mainContent.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = mainContent.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
mainContent.requestFocusFromTouch();
mainContent.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mainContent.loadUrl("javascript:window.HTMLOUT.processHTML(''+document.getElementsByTagName('html')[0].innerHTML+'');");
}
});
mainContent.setWebChromeClient(new WebChromeClient());
mainContent.loadUrl("file:///android_asset/"+filename.get(position));
webSettings.setDefaultFontSize(40);
在我的活动中
public void processHTML(String html) {
System.out.println("======++++"+Html.fromHtml(html));
}
我什么都没看到.如何获取字符串中HTML页面的内容(文本).我在做什么错?提前致谢
解决方法:
这样我就知道了
mainContent.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = mainContent.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
mainContent.requestFocusFromTouch();
mainContent.setWebChromeClient(new WebChromeClient());
mainContent.loadUrl("file:///android_asset/"+filename.get(position));
mainContent.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mainContent.setWebViewClient(null);
mainContent.loadUrl("javascript:window.HTMLOUT.processHTML('
在我的活动中
class MyJavaScriptInterface {
@SuppressWarnings("unused")
public void processHTML(final String html) {
runOnUiThread(new Runnable() {
public void run() {
Spanned page = Html.fromHtml(html);
System.out.println("content"+page);
}
});
}
}
标签:webview,html,android
来源: https://codeday.me/bug/20191029/1959319.html