在Android中使用Facebook API发布文本状态 - 最简单的方法

时间:2012-12-08 19:02:08

标签: android facebook-android-sdk

我一直在寻找一种方法来做到这一点。但似乎没有什么对我有用。有人可以帮忙这样做吗?

这是我状态帖子的图片按钮:

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/hoributtons"
    android:layout_alignTop="@+id/imageButton2"
    android:background="#00000000"
    android:contentDescription="@string/facebook"
    android:onClick="shareOnFacebook"
    android:src="@drawable/facebookbutton" />

这是我的mainactivity.java文件的相应部分:

public class MainActivity extends FacebookActivity {

    private static final String APP_ID = "xxxxxxxxxxxxx";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }



public void shareOnFacebook(View v) {
        //mfacebook = new Facebook("xxxxxxxxxxxxx");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

有人能指出正确的方向吗? :)

3 个答案:

答案 0 :(得分:11)

假设您想在自己的墙上张贴(因为问题不明确),这应该适合您。

public class MainActivity extends Activity {
        private static final String APP_ID = "xxxxxxxxxxxxx";
        private Facebook mFacebook;
        private AsyncFacebookRunner mAsyncRunner;
        private EditText yourEditText;
        private String toShare;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mFacebook = new Facebook();
            mAsyncRunner = new AsyncFacebookRunner(mFacebook);
            SessionEvents.addAuthListener(new SampleAuthListener());
            SessionEvents.addLogoutListener(new SampleLogoutListener());
            yourEditText = (EditText) findViewById(R.id.<youreditTextId>);
            toShare = yourEditText.getText().toString();
        }

        public void shareOnFacebook(View v) {
            Bundle params = new Bundle();
            params.putString("message", toShare);

            mAsyncRunner.request("me/feed", params, "POST", new RequestListener() {
                public void onMalformedURLException(MalformedURLException e) {}
                public void onIOException(IOException e) {}
                public void onFileNotFoundException(FileNotFoundException e) {}
                public void onFacebookError(FacebookError e) {}
                public void onComplete(String response) {
            }
        }); 

        Toast.makeText(MainActivity.this, "Posting to your Wall", Toast.LENGTH_SHORT).show();  

          }

     }

有关将图片发布到墙上的详细信息,Facebook有很好的文档。

答案 1 :(得分:2)

使用您的Android应用程序成功登录的用户墙上发布消息的最简单方法是(我的工作代码)_

public class AndroidFacebookWallPost extends Activity {

// Your Facebook APP ID:
private static String APP_ID = "your App ID here"; //

// Instance of Facebook Class:
private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;

// Your ImageButton that Post Message to Facebook Wall:
ImageButton btnPostToWall;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnPostToWall = (ImageButton) findViewById(R.id.imageButton1);// Your image button...

    facebook = new Facebook(APP_ID);
    mAsyncRunner = new AsyncFacebookRunner(facebook);
    // set listener for Post Message button
    btnPostToWall.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            postToWall();
        }
    });

}

/**
 * Method that Post a Text Status using Facebook API on user`s wall.
 */
public void postToWall() {
    // post on user's wall.
    facebook.dialog(this, "feed", new DialogListener() {

        @Override
        public void onFacebookError(FacebookError error) {
            Toast.makeText(AndroidFacebookWallPost.this, "Post fail "+error, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(DialogError error) {
            Toast.makeText(AndroidFacebookWallPost.this, "Post fail due to "+error, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onComplete(Bundle values) {
            Toast.makeText(AndroidFacebookWallPost.this, "Post success.", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCancel() {
            Toast.makeText(AndroidFacebookWallPost.this, "Cancle by user!", Toast.LENGTH_LONG).show();
        }
    });

}

}

了解更多请访问Getting Started with the Facebook SDK for Android

答案 2 :(得分:2)

以下是您可以捆绑的变量的详细信息 Bundle params = new Bundle();

params.putString("message", "This string will appear as the status message");
    params.putString("link", "This is the URL to go to");
    params.putString("name", "This will appear beside the picture");
    params.putString("caption", "This will appear under the title");
    params.putString("description", "This will appear under the caption");
    params.putString("picture", "This is the image to appear in the post");

使用AsyncFacebookRunner将数据发布到facebook:

mAsyncRunner.request("me/feed", params, "POST", new RequestListener() {
            public void onMalformedURLException(MalformedURLException e) {}
            public void onIOException(IOException e) {}
            public void onFileNotFoundException(FileNotFoundException e) {}
            public void onFacebookError(FacebookError e) {}
            public void onComplete(String response) {
        }
    });