0

我有以下脚本需要在浏览器中显示。我尝试使用 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。

谢谢,

西蒙

4

2 回答 2

0

我在这个链接上有答案。

“你错过了教程中他添加的部分

webView.setWebChromeClient(new WebChromeClient());

添加后立即

webView.getSettings().setJavaScriptEnabled(true);"
于 2013-10-22T17:25:31.740 回答
0

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

webview.getSettings().setJavaScriptEnabled(true);
于 2013-10-21T18:36:10.603 回答