1

我无法在用户的墙上设置一个简单的 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() {}
                });


              } 
            }

        });

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

4

1 回答 1

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());
                    }
                }
            }
        }
于 2012-08-03T10:33:13.440 回答