android facebook api post

前端 未结 1 1455
长发绾君心
长发绾君心 2021-02-06 16:14

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

1条回答
  •  你的背包
    2021-02-06 16:36

    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.

    0 讨论(0)
提交回复
热议问题