android facebook api post

12,917

I am assuming that you are doing that bit of code after the user successfully authenticates?

This bit of code worked for me:

private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner;

private void onFacebookShare() {
    mFacebook = new Facebook();
    mAsyncRunner = new AsyncFacebookRunner(mFacebook);

    SessionEvents.addAuthListener(new SampleAuthListener());
    SessionEvents.addLogoutListener(new SampleLogoutListener());
}

private void postToFBWall() {
    if(mFacebook.isSessionValid()){
        shareVideoOnFB();
    } else {
        showDialog(DIALOG_FBOOK_LOGIN);
    }
}

public void shareVideoOnFB(){
    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");

    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) {
            logoutFacebook();
        }
    }); 

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

You can call onFacebookShare() in your activity's onCreate(), and then when the user presses whatever to indicate that s/he wants to share on Facebook, call postToFBWall(). Of course you have to add in handling to show the login dialog.

Share:
12,917
Kelly Elton
Author by

Kelly Elton

int main(){return main();}

Updated on June 08, 2022

Comments

  • Kelly Elton
    Kelly Elton almost 2 years

    I have a problem. I want to use the facebook api and make a post to my wall without calling a dialog. Basically I have an app and I want people to be able to share the app, so I want to have a specific message to post. I keep getting a response of "Method not implemented". Heres the code for the post.

    //I tried this also ->>String path = "http://graph.facebook.com/me/feed";
    String path = "https://api.facebook.com/method/stream.publish";
    Bundle b = new Bundle();
    //And i tried this -> b.putString("access_token",facebook.getAccessToken());
    b.putString("message", "this is just a test...");
    try {
        String ret = facebook.request(path, b);
        Toast.makeText(fmasterActivity.this, ret, Toast.LENGTH_LONG).show();
        } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }