我正在尝试在本地WebView中显示HTML页面而没有Internet连接,但如果在HTML中添加脚本,则不会加载内容。 这是显示HTML的代码:
String htmlContent = "<html>\n" +
"<head>\n" +
"<link rel=\"stylesheet\" type=\"text/css\" href=\"file:///android_asset/combined_main.css\" /> " +
"<link rel=\"stylesheet\" type=\"text/css\" href=\"file:///android_asset/libs.css\" />" +
"<script type=\"text/javascript\" async src=\"file:///android_asset/MathJax.js\"></script>\n" +
"</head>" +
"<body>" +
content.getContentName() + //My Rest of the HTML content
"</body>" +
"</html>\n";
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadDataWithBaseURL("file:///android_asset/", htmlContent, "text/html", "UTF-8", null);
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.requestFocus(View.FOCUS_DOWN);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.setWebViewClient(new MyBrowser());
protected class WebAppInterface {
Context mContext;
/**
* Instantiate the interface and set the context
*/
WebAppInterface(Context c) {
mContext = c;
}
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
showLoading();
}
@Override
public void onPageFinished(final WebView view, String url) {
super.onPageFinished(view, url);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
view.scrollTo(0, 0);
}
}, 500);
hideLoading();
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
// super.onReceivedSslError(view, handler, error);
handler.proceed();
}
}
如果我删除了我的JS系列,则显示网页,否则显示不是。
任何帮助将不胜感激。提前谢谢。
答案 0 :(得分:1)
如果您使用此baseURL开始使用webview:
webView.loadDataWithBaseURL("file:///android_asset/"...
您不需要再次将路径放在HTML文件中
尝试
String htmlContent = "<html>\n" +
"<head>\n" +
"<link rel=\"stylesheet\" type=\"text/css\" href=\"combined_main.css\" /> " +
"<link rel=\"stylesheet\" type=\"text/css\" href=\"libs.css\" />" +
"<script type=\"text/javascript\" async src=\"MathJax.js\"></script>\n" +
"</head>" +
"<body>" +
content.getContentName() + //My Rest of the HTML content
"</body>" +
"</html>\n";