Webview android 无法加载 HTML5

Webview android couldn't load HTML5

我的Android应用程序升级到API29,所以我们需要从Xwalkcorelibrary更改为WebView,当运行应用程序可以显示HTML5但是内容此页面未显示。

当使用 WebView 时:

    setContentView(R.layout.bookshelf_view);
    mWebView = findViewById(R.id.web_view);
    mWebView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(request.getUrl().toString());
            return true;
        }
    });
    try {
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setUserAgentString(webSettings.getUserAgentString() + " " + AndroidCore.getInstance().getApiVersion());
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setAppCacheEnabled(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
    mWebView.loadUrl("file:///android_asset/HTML5/index.html");

使用Xwalkcorelibrary时:

 try {
        Method getBridge = XWalkView.class.getDeclaredMethod("getBridge");
        getBridge.setAccessible(true);
        XWalkViewBridge xWalkViewBridge = null;
        xWalkViewBridge = (XWalkViewBridge) getBridge.invoke(mXwalkView);
        XWalkSettings webSettings = xWalkViewBridge.getSettings();
        webSettings.setUserAgentString(
                webSettings.getUserAgentString() + " " + AndroidCore.getInstance().getApiVersion());
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setAppCacheEnabled(true);
    } catch (Exception e) {
        e.printStackTrace();
    }

    mXwalkView.setResourceClient(new MyResourceClient(mXwalkView));
    mXwalkView.setUIClient(new MyUIClient(mXwalkView));
    this.configureWebView();
    mXwalkView.loadAppFromManifest("file:///android_asset/manifest.json", null)

那我做错了什么?请帮我想出解决办法?

你有没有这样试过它在我的项目中对我有用:

WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    WebViewClientImpl webViewClient = new WebViewClientImpl(this);
    webView.setWebViewClient(webViewClient);


    webView.loadUrl("URL");

拦截 WebView HTTP 请求

public class WebViewClientImpl extends WebViewClient {

private Activity activity = null;

public WebViewClientImpl(Activity activity) {
    this.activity = activity;
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if(url.indexOf("jenkov.com") > -1 ) return false;

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    activity.startActivity(intent);
    return true;
}

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    if(url.startsWith("http://tutorials.jenkov.com/images/logo.png")){
        String mimeType = "image/png";
        String encoding = "";
        InputStream input = ...;


        WebResourceResponse response =
                new WebResourceResponse(mimeType, encoding, input);

        return response;
    }

    return null;
}
}

在 HTML 5 本地存储中缓存值

WebSettings webSettings = webView.getSettings();

webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);

android:hardwareAccelerated="true" 添加到您的清单文件。有时 webview 无法加载视频和 iframe hardwareAccelerated

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        android:hardwareAccelerated="true">

您也可以尝试更改文件 url -

尝试改变这个 - mWebView.loadUrl("file:///android_asset/HTML5/index.html");

mWebView.loadUrl("file:///android_asset/index.html");