繁体   English   中英

通过webView加载html / javascript

[英]loading html/javascript via webView

我有以下脚本需要在浏览器中显示。 我尝试使用WebView loadData和loadDataWithBaseURL加载,但没有成功(空视图)。 我可能会错过MIME,编码等功能。

<head>
<meta name='viewport' content='width=device-width'/>
<script type='text/javascript'     src='http://www.googletagservices.com/tag/js/gpt_mobile.js'></script>
    <script type='text/javascript'>eval(window.location.search.substring(1));googletag.cmd.push(function(){googletag.defineSlot('/7047/AppCamoli_Iphone_Transition_Splash',[320,480],'div-gpt-ad-2935936626049-0').addService(googletag.pubads());googletag.enableServices();});</script>
</head>
<body style="margin:0; padding: 0;">
<div id='div-gpt-ad-2935936626049-0'>
    <script type='text/javascript'>
        document.getElementById('div-gpt-ad-2935936626049-0').id='div-gpt-ad-2935936626049-0';googletag.cmd.push(function(){googletag.display('div-gpt-ad-2935936626049-0')});
    </script>
</div>
</body>

我的代码如下:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.web);

    try {
        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        Intent i = getIntent();
        boolean typeURL = i.getBooleanExtra("urlType", true);
        String url = i.getStringExtra("url");
        if (typeURL)
            webView.loadUrl(url);
        else {
            //String html = "<html><body>You scored <b>hello world</b> points.</body></html>";
            String html = "<html><head><meta name='viewport' content='width=device-width'/><script type='text/javascript' src='http://www.googletagservices.com/tag/js/gpt_mobile.js'></script><script type='text/javascript'>eval(window.location.search.substring(1));googletag.cmd.push(function(){googletag.defineSlot('/7047/AppCamoli_Iphone_Transition_Splash',[320,480],'div-gpt-ad-2935936626049-0').addService(googletag.pubads());googletag.enableServices();});</script></head><body style='margin:0; padding: 0;'><div id='div-gpt-ad-2935936626049-0'><script type='text/javascript'>document.getElementById('div-gpt-ad-2935936626049-0').id='div-gpt-ad-2935936626049-0';googletag.cmd.push(function(){googletag.display('div-gpt-ad-2935936626049-0')});</script></div></body></html>";
            //webView.loadDataWithBaseURL(null, html, "text/javascript", "UTF-8", null);
            webView.loadData(html, "text/javascript", "UTF-8");
        }
    } catch (Exception e) {
        Log.e("webView","failed to load URL");
        e.printStackTrace();
    }
}

这将导致一个空的webView。

谢谢,

西蒙

您是否尝试过修改WebSettings并启用JavaScript?

webview.getSettings().setJavaScriptEnabled(true);

我有这个链接的答案。

“您错过了本教程中添加的部分

webView.setWebChromeClient(new WebChromeClient());

在添加之后

webView.getSettings().setJavaScriptEnabled(true);"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM