How to post on facebook wall android-sdk: 4.0.0

Please, help

I have a hava code for pressing a button on a wall:

btnPostToWall.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { postToWall(); } }); public void postToWall() { // post on user 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 a new Faceboof android sdk 4.0.0 and this code is depreacated

How to write a new library on the wall?

I read this one , but I do not understand how to use

+5
source share
3 answers

This may not be exactly the solution you are looking for, but I am using it.

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

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

and the non-static private string message . So when you try to share something (like

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

) a new instance of ShareApi will be created with message = null , and your message will not be added.

Decision:

  • Open the ShareApi class if you use the Facebook SDK as an 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 use the Maven repository.

  • Change this code:

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

    to that:

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

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

    to that:

     public ShareApi(String message, final ShareContent shareContent) { this.message = message; this.shareContent = shareContent; this.graphNode = DEFAULT_GRAPH_NODE; } 
  • Use your modified ShareApi class to share 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 the text, you can use the code below for the content object:

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

You have already read this guide https://developers.facebook.com/docs/sharing/android and you can add different ShareContent to your post. Use the examples from the Facebook Github repository to better understand the new SDK.

PS Of course, you must have a valid access token and publish_actions .

+5
source

The official Facebook documentation on how to share with the Android SDK 4.0 is here:

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

This link has examples of how to share by invoking the Graph API or sharing, invoking the application’s native Facebook dialog box.

Here's how I applied the sharing dialog in my own application:

in xml for activity / fragment I added 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 meet with the Facebook dialog box, as you expected.

Hope this helps.

+6
source

This is a complete working example (02/13/2017), based on the maximum 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); } } 
+1
source

All Articles