无法通过本地HTML在Android WebView上呈现Facebook评论

时间:2013-03-23 06:51:12

标签: android android-activity facebook-javascript-sdk android-webview facebook-comments

我有一个简单的活动创建WebView来加载Facebook评论,例如

  protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView myWebView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        // myWebView.loadUrl("http://192.168.0.2/facebook.html"); // See 1st image
        myWebView.loadUrl("file:///android_asset/facebook.html"); // See 2nd image
    }

仅对远程html文件的调用有效,但本地调用不起作用。请参阅以下图像:

通过远程文件

via remote file

通过本地文件

via local file

facebook.html

的内容
![<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>

<div id="fb-root"></div>
<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 = "http://connect.facebook.net/en_US/all.js#xfbml=1&appId=xxx";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

<div class="fb-comments" data-href="http://example.com" data-width="470" data-num-posts="10"></div>

  </body>
</html>

1 个答案:

答案 0 :(得分:6)

您需要指定基本网址。

myWebView.loadDataWithBaseURL("http://www.example.com", "YOUR_HTML", "text/html", null, null)

完全替换上面的一行:

BufferedReader reader = new BufferedReader(new FileReader("file:///android_asset/facebook.html"));
String line;
String html = "";
while((line = reader.readLine()) != null) {
    html += line;
}
reader.close();
myWebView.loadDataWithBaseURL("http://www.example.com", html, "text/html", null, null)