Android中的Facebook评论框

时间:2013-10-01 05:28:25

标签: android facebook

在我的应用中,我想通过webview在我的网站上显示评论。 这就是我所做的:

在assets文件夹中,我有一个名为comment.html的文件

<html>
<head>
</head>
<body>
<div id="fb-root"></div>
<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=467390796664437";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-comments" data-href="http://www.haivl.com/photo/1259735" data-width="470"></div>
</body>
</html>
  • 在CommentViewActivity.java

    public class CommentViewActivity extends Activity {     public static Context context;     私人WebView webView;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_zini_login);
        context = this;
        CookieSyncManager.createInstance(this);
        CookieManager cm = CookieManager.getInstance();
        cm.removeAllCookie();
        webView = (WebView) findViewById(R.id.web_login);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("file:///android_asset/comment.html");
        webView.setWebViewClient(new WebViewClientActivity());
    }
    
    public class WebViewClientActivity extends WebViewClient {
        @Override
          public void onPageStarted(WebView view, String url, Bitmap favicon) {
            System.out.println("onPageStarted: " + url);
    
          }
    
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    
        @Override
          public void onPageFinished(WebView webView, String url) {
            System.out.println("onPageFinished: " + url);
    
          }
    }
    

    }

活动显示如下: enter image description here

正如您所看到的,Facebook评论框模糊不清,我看不到整个评论。 有人有想法吗?

感谢您的关注!

1 个答案:

答案 0 :(得分:1)

基于这篇文章Failed to render Facebook comments on Android WebView via local HTML

您需要指定基本网址。

myWebView.loadDataWithBaseURL("http://www.haivl.com", 
"<html>
<head>
</head>
<body>
<div id="fb-root"></div>
<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=467390796664437";
 fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-comments" data-href="http://www.haivl.com/photo/1259735" data-width="470"></div> 
</body>
</html>", "text/html", null, null);