Webview android无法加载HTML5

时间:2019-11-26 04:02:37

标签: android android-webview xwalkview

我的Android应用程序已升级到API 29,因此,当运行的应用程序可以显示HTML5但此页面的内容未显示时,我们需要从Xwalkcorel库更改为WebView。

使用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)

那我做错了什么?请帮我找出解决方案吗?

2 个答案:

答案 0 :(得分:1)

您是否尝试过这样的方法?它在我的项目中为我工作:

pip install more-itertools==5.0.0

拦截WebView HTTP请求

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

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


    webView.loadUrl("URL");

在HTML 5本地存储中缓存值

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;
}
}

答案 1 :(得分:0)

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

<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");