Android:Facebook SDK - 分享帖子的描述,缩略图和标题

时间:2012-08-03 10:17:37

标签: android facebook facebook-stream-story

我在设置一个简单的Facebook墙贴到用户墙上时遇到了麻烦。 我想在点击带有缩略图,描述和标题的按钮时弹出一个facebook对话框。 我尝试了以下代码,但没有弹出对话框:

shareOnFacebookBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                /*
                 * Get existing access_token if any
                 */
                mPrefs ShopDetailActivity.this.getActivity().getPreferences(Context.MODE_PRIVATE);
                String access_token = mPrefs.getString("access_token", null);
                long expires = mPrefs.getLong("access_expires", 0);
                if(access_token != null) {
                    facebook.setAccessToken(access_token);
                }
                if(expires != 0) {
                    facebook.setAccessExpires(expires);
                }

                /*
                 * Only call authorize if the access_token has expired.
                 */
                if(!facebook.isSessionValid()) {    

                facebook.authorize(ShopDetailActivity.this.getActivity(),new String[] { "publish_stream" }, new DialogListener() {
                    @Override
                    public void onComplete(Bundle values) {

                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token", facebook.getAccessToken());
                        editor.putLong("access_expires", facebook.getAccessExpires());
                        editor.commit();
                        //facebook.dialog(ShopDetailActivity.this.getActivity(), "feed", new SampleDialogListener());
                        Bundle parameters = new Bundle();
                        parameters.putString("message", "message");// the message to post to the wall
                        facebook.dialog(context, "feed", parameters, this);

                    }

                    @Override
                    public void onFacebookError(FacebookError error) {}

                    @Override
                    public void onError(DialogError e) {}

                    @Override
                    public void onCancel() {}
                });


              } 
            }

        });

授权窗口打开,点击后,我希望弹出对话框,但它只是返回到应用程序。 我做错了什么?

1 个答案:

答案 0 :(得分:2)

facebook = new Facebook("your facebook id");

                    mAsyncRunner = new AsyncFacebookRunner(facebook);
                    facebook.authorize(this, new String[]
                    { "publish_stream", "offline_access" }, -1,

                    new DialogListener()
                    {
                        public void onComplete(Bundle values)
                        {
                            Log.e("tag", "Values returned by Bundle ====> " + values.toString());
                            fbImageSubmit();
                        }

                        public void onFacebookError(FacebookError error)
                        {

                        }

                        public void onError(DialogError e)
                        {

                        }

                        public void onCancel()
                        {

                        }
                    });

//add method into your class

    private void fbImageSubmit()
        {
            if (fb != null)
            {
                if (fb.isSessionValid())
                {
                    Bundle b = new Bundle();
                  b.putString("picture", your image url);
                    b.putString("caption", title);
                    b
                            .putString(
                                    "description",
                                    "test");
                    b.putString("name", "Hi Friends, I am using the your app name app for Android!");
                    b.putString("link", "https://market.android.com/details?id="+this.getApplication().getPackageName().toString());
                    try
                    {
                        String strRet = "";
                        strRet = fb.request("/me/feed", b, "POST");
                        JSONObject json;
                        try
                        {
                            json = Util.parseJson(strRet);
                            if (!json.isNull("id"))
                            {
                                Log.i("Facebook", "Image link submitted.");
                            }
                            else
                            {
                                Log.e("Facebook", "Error: " + strRet);
                            }
                        } catch (FacebookError e)
                        {
                            Log.e("Facebook", "Error: " + e.getMessage());
                        }
                    } catch (Exception e)
                    {
                        Log.e("Facebook", "Error: " + e.getMessage());
                    }
                }
            }
        }