toni7777
toni7777

Reputation: 99

How post to wall facebook android-sdk:4.0.0

Please help

I hava code for button post to wall :

btnPostToWall.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        postToWall();
    }
});

public void postToWall() {
    // post on user's wall.
    facebook.dialog(this, "feed", new DialogListener() {

        @Override
        public void onFacebookError(FacebookError e) {
        }

        @Override
        public void onError(DialogError e) {
        }

        @Override
        public void onComplete(Bundle values) {
        }

        @Override
        public void onCancel() {
        }
    });

}

But I Have New Faceboof android sdk 4.0.0 and this code depreacated

How post to wall whith new library?

I read this, but I don't understand how to use

Upvotes: 5

Views: 13444

Answers (3)

Michalsx
Michalsx

Reputation: 3621

This is full working (13.02.2017) example based on Max answer.

Gradle: compile 'com.facebook.android:facebook-android-sdk:[4,5)'

public class ShareOnFacebook extends Activity {

    private static final String TAG = "ShareOnFacebook";

    CallbackManager callbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        shareOnWall();
    }

    void shareOnWall() {
        ShareDialog shareDialog = new ShareDialog(this);
        callbackManager = CallbackManager.Factory.create();
        shareDialog.registerCallback(callbackManager, new

                FacebookCallback<Sharer.Result>() {
                    @Override
                    public void onSuccess(Sharer.Result result) {
                        Log.d(TAG, "onSuccess: ");
                        Toast.makeText(ShareOnFacebook.this, "onSuccess", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel() {
                        Log.d(TAG, "onCancel: ");
                        Toast.makeText(ShareOnFacebook.this, "onCancel", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(FacebookException error) {
                        Log.d(TAG, "onError: ");
                        Toast.makeText(ShareOnFacebook.this, "onError" + error.toString(), Toast.LENGTH_SHORT).show();
                    }
                });

        if (ShareDialog.canShow(ShareLinkContent.class)) {
            ShareLinkContent linkContent = new ShareLinkContent.Builder()
                    .setContentTitle("Hello Facebook")
                    .setContentDescription("The 'Hello Facebook' sample  showcases simple Facebook integration")
                    .setContentUrl(Uri.parse("http://developers.facebook.com/android"))
                    .build();

            shareDialog.show(linkContent);
        }
    }

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }


}

Upvotes: 1

Max Worg
Max Worg

Reputation: 2972

The official Facebook documentation on how to share from Android SDK 4.0 is located here:

https://developers.facebook.com/docs/sharing/android

That link has examples of how to share by calling the Graph API or sharing by calling the native Facebook app dialog.

Here is how I implemented the share dialog in my own app:

in the xml for the activity/fragment I added the Button

  <Button
        android:layout_width="144dp"
        android:layout_height="144dp"
        android:id="@+id/shareFacebookButton"
        android:text=""
        android:background="@drawable/facebook_button"
        android:layout_gravity="center"
        android:layout_marginBottom="6dp"
        />

Then inside the Fragment:

Button shareButton = (Button)view.findViewById(R.id.shareFacebookButton);
shareDialog = new ShareDialog(this);
callbackManager = CallbackManager.Factory.create();
shareDialog.registerCallback(callbackManager, new 

FacebookCallback<Sharer.Result>() {
    @Override
    public void onSuccess(Sharer.Result result) {}

    @Override
    public void onCancel() {}

    @Override
    public void onError(FacebookException error) {}
});

shareButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
         if (ShareDialog.canShow(ShareLinkContent.class)) {
             ShareLinkContent linkContent = new ShareLinkContent.Builder()
                .setContentTitle("Hello Facebook")
                .setContentDescription("The 'Hello Facebook' sample  showcases simple Facebook integration")

             .setContentUrl(Uri.parse("http://developers.facebook.com/android"))
                            .build();

         shareDialog.show(linkContent);
          }
     }});

Now when someone clicks on the button they will be met with the Facebook dialog like you would expect.

Hope this helps.

Upvotes: 5

Pavlo28
Pavlo28

Reputation: 1596

Maybe it's not quite the solution you are looking for, but I'm using it.

Facebook Android SDK 4 has class ShareApi for sharing your content. This class has static method share():

public static void share(
            final ShareContent shareContent,
            final FacebookCallback<Sharer.Result> callback) {
        new ShareApi(shareContent)
                .share(callback);
    }

and non static private String message. So when you're trying to share something (for ex.

ShareApi api = new ShareApi(content);
api.setMessage("My message");
api.share(content, new FacebookCallback<Sharer.Result>() ...)

) will be created new instance of ShareApi with message = null and your message won't be added.

The solution:

  1. Open class ShareApi if you're using Facebook SDK as external library OR copy this class from Github https://github.com/facebook/facebook-android-sdk/blob/master/facebook/src/com/facebook/share/ShareApi.java if you're using Maven repository.

  2. Change this code:

    public static void share(
        final ShareContent shareContent,
        final FacebookCallback<Sharer.Result> callback) {
    new ShareApi(shareContent)
            .share(callback);
    }
    

    to this one:

    public static void share(final String message,
        final ShareContent shareContent,
        final FacebookCallback<Sharer.Result> callback) {
    new ShareApi(message, shareContent)
            .share(callback);
    }
    
  3. Change this code:

    public ShareApi(final ShareContent shareContent) {
        this.shareContent = shareContent;
        this.graphNode = DEFAULT_GRAPH_NODE;
    }
    

    to this one:

    public ShareApi(String message, final ShareContent shareContent) {
        this.message = message;
        this.shareContent = shareContent;
        this.graphNode = DEFAULT_GRAPH_NODE;
    }
    
  4. Use your changed ShareApi class for sharing your content:

    ShareApi.share("My message", content, new FacebookCallback<Sharer.Result>() {
    
            @Override
            public void onSuccess(Sharer.Result result) {
                if (AppConfig.DEBUG) {
                    Log.d(TAG, "SUCCESS");
                }
            }
    
            @Override
            public void onCancel() {
                if (AppConfig.DEBUG) {
                    Log.d(TAG, "CANCELLED");
                }
            }
    
            @Override
            public void onError(FacebookException error) {
                if (AppConfig.DEBUG) {
                    Log.d(TAG, error.toString());
                }
            }
        });
    

If you just want to share text, you can use code below for content object:

ShareLinkContent content = new ShareLinkContent.Builder()
                .build();

You've already read this manual https://developers.facebook.com/docs/sharing/android and can add different ShareContent to your post. Use examples from Facebook Github repository for better understanding new SDK.

P.S. Of course, you should have valid access token and publish_actions permission.

Upvotes: 5

Related Questions