webview加载网页,但加载后某些内容消失(Android Studio) [英] webview loads webpage, but some contents get vanished after loading (Android Studio)

查看:328
本文介绍了webview加载网页,但加载后某些内容消失(Android Studio)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在网络视图中查看 http://artikelweb.com . 网页显示得很好.但是,每当我转到热门作者"部分的任何作者链接时,都会显示网页,但是加载后,引号不会显示.

I tried to view http://artikelweb.com in a webview. The web page appears nicely. But, whenever I go to any author link from "Popular Authors" section, the web page appears, but, after loading, the quotes aren't showing.

在Google Chrome浏览器(移动版)中,引号在加载后显示,

In Google Chrome Browser(mobile) the quotes appear after loading,

但是,在我的应用中,引号未在网络视图中显示.

but, in my app, the quotes are not showing in web view.

代码段:

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web);

    myWebView = (WebView)findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("http://www.artikelweb.com");
    myWebView.setWebViewClient(new WebViewClient());


}


@Override
public void onBackPressed() {
    if(myWebView.canGoBack()) {
        myWebView.goBack();
    } else {
        super.onBackPressed();
    }
}

推荐答案

在您的特定情况下,您必须启用DOM Storage API

In your particular case you must enable the DOM Storage API

webSettings.setDomStorageEnabled(true);

因此您的代码必须变为:

So your code must become:

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web);

    myWebView = (WebView)findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    myWebView.loadUrl("http://www.artikelweb.com");
    myWebView.setWebViewClient(new WebViewClient());
}

因为使用正在访问的网站的javascript需要在其javascript中使用它.

That becouse the javascript that use the website you are accessing needs it in its javascript.

您当然可以利用第三方库,但是您仍然不知道为什么这种情况适用于第三方库而不是默认的Webview.

Of course you could take advantage of a third party library, but again you won't know why this exact case works in the third party one and not in the default webview.

您使用的库具有以下初始设置:

The library you are using has this initial settings:

https://raw.githubusercontent.com/delight-im/Android-AdvancedWebView/master/Source/library/src/main/java/im/delight/android/webview/AdvancedWebView.java

final WebSettings webSettings = getSettings();
        webSettings.setAllowFileAccess(false);
        setAllowAccessFromFileUrls(webSettings, false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        if (Build.VERSION.SDK_INT < 18) {
            webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        }
        webSettings.setDatabaseEnabled(true);
        if (Build.VERSION.SDK_INT < 19) {
            webSettings.setDatabasePath(databaseDir);
        }
        setMixedContentAllowed(webSettings, true);

        setThirdPartyCookiesEnabled(true);

出于安全原因,默认启用默认Web视图已禁用的许多选项.

Enabling by default many options that the default webview has disable for security reason.

这篇关于webview加载网页,但加载后某些内容消失(Android Studio)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆