java 如何发布到墙 facebook android-sdk:4.0.0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30224390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How post to wall facebook android-sdk:4.0.0
提问by toni7777
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
但是我有新的 Faceboof android sdk 4.0.0 并且此代码已弃用
How post to wall whith new library?
如何在新图书馆的墙上张贴?
I read this, but I don't understand how to use
我读了这个,但我不明白如何使用
采纳答案by GrafOrlov
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()
:
Facebook Android SDK 4 具有ShareApi
用于共享您的内容的类。这个类有静态方法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.
和非静态私有 String message
。因此,当您尝试分享某些内容时(例如。
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.
) 将创建ShareApi
with 的新实例,message = null
并且不会添加您的消息。
The solution:
解决方案:
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.javaif you're using Maven repository.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); }
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; }
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()); } } });
ShareApi
如果您使用 Facebook SDK 作为外部库,请打开类或从 Github https://github.com/facebook/facebook-android-sdk/blob/master/facebook/src/com/facebook/share/ShareApi复制此类。 java如果您使用的是 Maven 存储库。更改此代码:
public static void share( final ShareContent shareContent, final FacebookCallback<Sharer.Result> callback) { new ShareApi(shareContent) .share(callback); }
到这个:
public static void share(final String message, final ShareContent shareContent, final FacebookCallback<Sharer.Result> callback) { new ShareApi(message, shareContent) .share(callback); }
更改此代码:
public ShareApi(final ShareContent shareContent) { this.shareContent = shareContent; this.graphNode = DEFAULT_GRAPH_NODE; }
到这个:
public ShareApi(String message, final ShareContent shareContent) { this.message = message; this.shareContent = shareContent; this.graphNode = DEFAULT_GRAPH_NODE; }
使用更改后的
ShareApi
课程分享您的内容: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:
如果您只想共享文本,可以使用以下代码作为content
对象:
ShareLinkContent content = new ShareLinkContent.Builder()
.build();
You've already read this manual https://developers.facebook.com/docs/sharing/androidand can add different ShareContent
to your post. Use examples from Facebook Github repositoryfor better understanding new SDK.
您已经阅读了本手册https://developers.facebook.com/docs/sharing/android并且可以ShareContent
在您的帖子中添加不同的内容。使用Facebook Github 存储库中的示例更好地理解新 SDK。
P.S. Of course, you should have valid access token and publish_actions
permission.
PS 当然,您应该拥有有效的访问令牌和publish_actions
权限。
回答by Max Worg
The official Facebook documentation on how to share from Android SDK 4.0 is located here:
有关如何从 Android SDK 4.0 共享的官方 Facebook 文档位于此处:
https://developers.facebook.com/docs/sharing/android
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.
该链接提供了如何通过调用 Graph API 进行共享或通过调用本机 Facebook 应用程序对话框进行共享的示例。
Here is how I implemented the share dialog in my own app:
以下是我在自己的应用程序中实现共享对话框的方式:
in the xml for the activity/fragment I added the Button
在活动/片段的 xml 中,我添加了 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.
现在,当有人点击按钮时,他们会看到像您期望的那样的 Facebook 对话框。
Hope this helps.
希望这可以帮助。
回答by Michalsx
This is full working (13.02.2017) example based on Max answer.
这是基于 Max answer 的完整工作 (13.02.2017) 示例。
Gradle: compile 'com.facebook.android:facebook-android-sdk:[4,5)'
Gradle:编译'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);
}
}