Facebook sdk:使用params caption android发布url到“你脑子里的东西”

时间:2013-07-16 05:13:41

标签: android facebook-android-sdk

我正在使用facebook sdk向facebook发布“你心目中的东西”的网址,但是当我在facebook帐户上查看时,没有发布任何内容。有人可以告诉我如何使用标题来使用facebook sdk将facebook发布到Facebook墙上。

/**
     * Function to login into facebook
     * */
    public void loginToFacebook() {

        mPrefs = getPreferences(MODE_PRIVATE);
        String access_token = mPrefs.getString("access_token", null);
        long expires = mPrefs.getLong("access_expires", 0);

        if (access_token != null) {
            Constants.facebook.setAccessToken(access_token);

            /*
            btnFbLogin.setVisibility(View.INVISIBLE);

            // Making get profile button visible
            btnFbGetProfile.setVisibility(View.VISIBLE);

            // Making post to wall visible
            btnPostToWall.setVisibility(View.VISIBLE);

            // Making show access tokens button visible
            btnShowAccessTokens.setVisibility(View.VISIBLE);
                */
            Log.d("FB Sessions", "" + Constants.facebook.isSessionValid());

        }

        if (expires != 0) {
            Constants.facebook.setAccessExpires(expires);
        }

        if (!Constants.facebook.isSessionValid()) {
            Constants.facebook.authorize(this,
                    new String[] { "email", "publish_stream" },
                    new DialogListener() {

                        @Override
                        public void onCancel() {
                            // Function to handle cancel event
                        }

                        @Override
                        public void onComplete(Bundle values) {
                            // Function to handle complete event
                            // Edit Preferences and update facebook acess_token
                            SharedPreferences.Editor editor = mPrefs.edit();
                            editor.putString("access_token",
                                    Constants.facebook.getAccessToken());
                            editor.putLong("access_expires",
                                    Constants.facebook.getAccessExpires());
                            editor.commit();

                            // Making Login button invisible
                            /*btnFbLogin.setVisibility(View.INVISIBLE);

                            // Making logout Button visible
                            btnFbGetProfile.setVisibility(View.VISIBLE);

                            // Making post to wall visible
                            btnPostToWall.setVisibility(View.VISIBLE);

                            // Making show access tokens button visible
                            btnShowAccessTokens.setVisibility(View.VISIBLE);
                        */}

                        @Override
                        public void onError(DialogError error) {
                            // Function to handle error

                        }

                        @Override
                        public void onFacebookError(FacebookError fberror) {
                            // Function to handle Facebook errors

                        }

                    });
        }
    }

postonWall( “http://google.com/”);

public void postOnWall(String msg) {
     try {
            String response = facebook.request("me");
            Bundle parameters = new Bundle();
            parameters.putString("caption",url);   

           // parameters.putString("caption", "Test Caption: http://google.com/");
           // parameters.putString("method", "stream.publish");
            response = facebook.request("me/feed", parameters, 
                    "POST");
            Log.d("Tests", "got response: " + response);
            if (response == null || response.equals("") || 
                    response.equals("false")) {
               Log.v("Error", "Blank response");
            }
     } catch(Exception e) {
         e.printStackTrace();
     }
}

1 个答案:

答案 0 :(得分:1)

我检查你的代码,确保你的会话有效。 异常抛出:“缺少消息或附件”。 因此,请通过以下请求传递“消息”。

public void postOnWall(String msg) {
     try {
            String response = facebook.request("me");
            Bundle parameters = new Bundle();
            parameters.putString("caption",msg);
            parameters.putString("message","message1");
            response = facebook.request("me/feed", parameters, 
                    "POST");
            Log.d("System out", "got response: " + response);
            if (response == null || response.equals("") || 
                    response.equals("false")) {
               Log.e("System out", "Blank response");
            }
     } catch(Exception e) {
         e.printStackTrace();
     }
}

see more parameters

希望它能帮助你:)