我想在我的Android应用的Webview中嵌入Facebook帖子。他们展示了如何在网站中包含javascript(https://developers.facebook.com/docs/plugins/embedded-posts)
我尝试将html代码加载到webview中,如下所示:
String webcode = "<html>\n" +
" <body>\n" +
" <div class=\"fb-post\" \n" +
" data-href=\"https://www.facebook.com/20531316728/posts/10154009990506729/\"\n" +
" data-width=\"500\"></div>\n" +
" <!-- <div id=\"fb-root\"></div> -->\n" +
" </body>\n" +
"<script>(function(d, s, id) {\n" +
" var js, fjs = d.getElementsByTagName(s)[0];\n" +
" if (d.getElementById(id)) return;\n" +
" js = d.createElement(s); js.id = id;\n" +
" js.src = \"https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v2.5\";\n" +
" fjs.parentNode.insertBefore(js, fjs);\n" +
"}(document, 'script', 'facebook-jssdk'));</script>\n" +
"</html>";
web_view.getSettings().setJavaScriptEnabled(true);
web_view.loadData(webcode, "text/html", null);
清晰格式的Java脚本代码:
<html>
<body>
<div class="fb-post"
data-href="https://www.facebook.com/20531316728/posts/10154009990506729/"
data-width="500"></div>
<!-- <div id="fb-root"></div> -->
</body>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v2.5";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
</html>
但是,webview显示了两行奇怪的字符。如果此代码在服务器上运行,则Facebook帖子显示正确。需要做些什么才能在Android应用的webview中使用它?谢谢!
答案 0 :(得分:2)
使用web_view.loadDataWithBaseURL("http://some.url", webcode, "text/html", "utf-8", null);
代替web_view.loadData(webcode, "text/html", null);