How to use Facebook like button in Android application?

我的梦境 提交于 2019-12-24 15:33:02

问题


I am using the following code to integrate the LIKE functionality in facebook.com with my Android app.

First: I used an html file called FacebookLikeView.html and I placed it in "assets/www/" folder, and this is what I used in it:

<html xmlns:fb="http://ogp.me/ns/fb#">
<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 ="//connect.facebook.net/en_GB/all.js#xfbml=1&appId=My APP ID";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
</body>
<fb:like href="https://www.facebook.com/pages/OmarTalkcom/52242794400"    send="true" width="120" show_faces="true" font="arial">
</fb:like>
</html>

and then I used a button to launch the html file in a webView like this:

   Button like = (Button) findViewById(R.id.button1);
    like.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            WebView myWebView = (WebView) findViewById(R.id.webView1);
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            myWebView.loadUrl("file:///android_asset/www/FacebookLikeView.html");
        }
    });

But nothing happend and in the log I got the following when I click the button:

 04-28 09:33:56.940: D/CONTEXT(31684): nativeDraw called here 
 04-28 09:33:56.940: D/CONTEXT(31684): PictureSet* draw called here 
 04-28 09:33:56.940: V/(31684): In ResourceHandle::create newHandle
 04-28 09:33:56.990: D/dalvikvm(31684): GC_CONCURRENT freed 103K, 4% free     7195K/7431K, paused 8ms+3ms
 04-28 09:33:56.990: D/CONTEXT(31684): nativeDraw called here 
 04-28 09:33:56.990: D/CONTEXT(31684): PictureSet* draw called here 
 04-28 09:33:57.080: D/CONTEXT(31684): nativeDraw called here 
 04-28 09:33:57.080: D/CONTEXT(31684): PictureSet* draw called here 

So what am I doing wrong here or what is the right thing to do in order to like a facebook page from Android app ?

Note I put the above html code in html file and opened it with chrome and firefox but it didn't work, But it worked when I copied it to the following link in w3school html sample.


回答1:


<html xmlns:fb="http://ogp.me/ns/fb#">
<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 ="https://connect.facebook.net/en_US/all.js#xfbml=1&appId=your app id";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
</body>
<fb:like href="https://www.facebook.com/pages/OmarTalkcom/52242794400"    send="true" width="120" show_faces="true" font="arial">
</fb:like>
</html>

Note:You have to fill your facebook app id and I changed the js.src to https://connect.facebook.net/en_US/all.js




回答2:


Go throw with following sample example ...

https://github.com/crowjdh/android-facebook-like-button-integration

Try native facebook like button

https://developers.facebook.com/docs/android/like-button

Reference https://developers.facebook.com/docs/reference/android/current/class/LikeView



来源:https://stackoverflow.com/questions/16260322/how-to-use-facebook-like-button-in-android-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!