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