在Facebook墙壁上发布文本消息

时间:2012-07-18 10:28:06

标签: android facebook post facebook-android-sdk facebook-requests

我在墙上发短信时遇到了问题。 我预计应用程序将登录到fb并在墙上发布短信,但我的墙上没有发布任何内容。

以下是我的代码 登录部分与Facebook指南相同,我刚刚添加了“发布流”权限

public class FB_trial extends Activity {
Facebook facebook = new Facebook("app_id");
private AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);  
private SharedPreferences mPrefs;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fb_trial);
    /*
     * The following is tutorial sample code
     */
    mPrefs = getPreferences(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(this, 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();
            }
            @Override
            public void onFacebookError(FacebookError error) {}
            @Override
            public void onError(DialogError e) {}
            @Override
            public void onCancel() {}
        });
    }
    //end of the login sample code

    //post wall fx
    Bundle params = new Bundle();
    params.putString("message", "Fb testing message");
    params.putString("access_token", access_token);
    mAsyncRunner = new AsyncFacebookRunner(facebook);
    mAsyncRunner.request("me/feed", params, "POST", new RequestListener(){
        @Override
        public void onComplete(String response, Object state) {
            System.out.println("post wall success");
        }
        @Override
        public void onIOException(IOException e, Object state) {}
        @Override
        public void onFileNotFoundException(FileNotFoundException e,
                Object state) {}
        @Override
        public void onMalformedURLException(MalformedURLException e,
                Object state) {}
        @Override
        public void onFacebookError(FacebookError e, Object state) {}
    }, null);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_fb_trial, menu);
    return true;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    facebook.authorizeCallback(requestCode, resultCode, data);
}

}

日志中显示“wall post success”消息(这表示请求已完成?) 但我找不到我墙上的帖子。

1 个答案:

答案 0 :(得分:1)

您可以使用该对话框发布到墙上:

        Bundle params = new Bundle();
        params.putString("link", linkgame);
        params.putString("name", subject);
        params.putString("description", content);
        params.putString("picture", iconUrl);

        facebook.dialog(((Activity) ctx).getParent(), "feed", params,
                new DialogListener() {

                    public void onComplete(Bundle values) {
                    }

                    public void onFacebookError(FacebookError error) {}
                    public void onError(DialogError e) {}
                    public void onCancel() {}
        });