如何在Android中实现facebook评论插件

时间:2015-09-15 10:42:56

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

如何在Android中实现Facebook评论插件,允许用户将评论发布到他们的墙上,如下图所示?

https://i.stack.imgur.com/9CVFv.png

1 个答案:

答案 0 :(得分:4)

也许这不是你想要的,但我发现你可以从像图谱API Facebook social plugin on android或在WebView Android unable to implement facebook comment in a webview due to default browser

中开始滚动的一些想法

编辑:如果您设置了已有评论的正确域和路径,则应该有效。 如果原始路径没有注释,则没有检查它将如何显示

package com.example.ff;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
// changeable public variables 
    public static String APP_KEY = "You're app key";
    public static String BASE_DOMAIN = "http://www.9gag.com";
    public static String PATH_URL = "/tv/p/anNBpr";

    private WebView webView;
    private MainActivity context;



    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        CookieSyncManager.createInstance(this);
        CookieManager cm = CookieManager.getInstance();
        cm.removeAllCookie();
        webView = (WebView) findViewById(R.id.web_login);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.loadDataWithBaseURL(BASE_DOMAIN, 
                "<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="+APP_KEY+ "\";fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));</script><div class=\"fb-comments\" data-href=\""
        +BASE_DOMAIN+PATH_URL+"\" data-width=\"470\"></div> </body></html>", "text/html", null, null);
        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);

        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        return super.onOptionsItemSelected(item);
    }