使用Android Facebook SDK 3.0共享链接和文本

8

我正在尝试升级到Facebook SDK 3.0,并最终通过Request.newStatusUpdateRequest()使一切顺利。然而,我的应用程序除了链接外还分享/发布文本。我已经尝试/研究了以下内容:

Request.newStatusUpdateRequest()

这似乎没有任何Bundle或其他包含链接和图标的方式的选项。

Request.newRestRequest()

我跳过了这个内容,因为我看到REST被弃用了。

new WebDialog.FeedDialogBuilder(_activity, session, params).build().show();

实际上这很有效,但生成的帖子似乎未与我的Facebook应用程序链接,我不确定这将如何影响我的Facebook数据分析。

Request.newPostRequest()

根据我所阅读的内容,这种方法似乎是正确的。然而,我无法弄清楚从哪里获取GraphObject以作为参数之一传递。

如何正确地将文本、链接和图片发布/分享到用户的墙上?看起来应该使用Request.newPostRequest(),因此我将包含我拥有的代码。

Request request = Request.newPostRequest(session, "me/feed", ??graph_object??, new Request.Callback() {
    @Override
    public void onCompleted(Response response) {
        showPublishResult("message", response.getGraphObject(), response.getError());
    }
});
request.setParameters(params);
Request.executeBatchAsync(request);

但是究竟什么是GraphObject?我该如何获取graph_object?我阅读了很多关于GraphObject/OpenGraph/Graph API的FB文档,却越来越糊涂。

如果我完全走错了方向,请告诉我。如果Request.newPostRequest是正确的方法,请给我更多关于GraphObject参数的信息。

3个回答

15

最终通过使用以下方法,我成功地使用Facebook SDK 3.0获得了所需的一切:

Bundle params = new Bundle();
params.putString("caption", "caption");
params.putString("message", "message");
params.putString("link", "link_url");
params.putString("picture", "picture_url");

Request request = new Request(Session.getActiveSession(), "me/feed", params, HttpMethod.POST);
request.setCallback(new Request.Callback() {
    @Override
    public void onCompleted(Response response) {
        if (response.getError() == null) {
            // Tell the user success!
        }
    }
});
request.executeAsync();

我有同样的问题。与使用newPostRequest(..openGraphObject..)相比,仅使用“普通”的请求似乎对我来说有点蛮力,但是我越看越得出结论,大多数人只是按照你上面写的做法去做。 - JRun
编译错误:构造函数Request(Session, String, Bundle, HttpMethod)未定义。 - kgandroid
Request类不再可以被访问了。现在它被放到了一个私有包中。 - denvercoder9
@RafiduzzamanSonnet 这个不能用,那么现在如何与 fb sdk 3.0 共享呢? - Srishti Roy
@SrishtiRoy 阅读 Facebook 文档 将会有所帮助。 - denvercoder9

4

我是用这种方法做的。 看看这是否有帮助。

public static void publishFeedDialog(final Activity current, final String title,
        final String caption, final String description, final String link,
        final String pictureUrl) {
    // start Facebook Login
    Session.openActiveSession(current, true, new Session.StatusCallback() {

        // callback when session changes state
        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            if (session.isOpened()) {
                Bundle params = new Bundle();
                params.putString("name", title);
                params.putString("caption", caption);
                params.putString("description", description);
                params.putString("link", link);
                params.putString("picture", pictureUrl);

                WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(
                        current, Session.getActiveSession(), params))
                        .setOnCompleteListener(new OnCompleteListener() {

                            @Override
                            public void onComplete(Bundle values,
                                    FacebookException error) {
                                if (error == null) {
                                    // When the story is posted, echo the
                                    // success
                                    // and the post Id.
                                    final String postId = values
                                            .getString("post_id");
                                    if (postId != null) {
                                        ToastHelper.MakeShortText("Posted");
                                    } else {
                                        // User clicked the Cancel button
                                        ToastHelper
                                                .MakeShortText("Publish cancelled");
                                    }
                                } else if (error instanceof FacebookOperationCanceledException) {
                                    // User clicked the "x" button
                                    ToastHelper
                                            .MakeShortText("Publish cancelled");
                                } else {
                                    // Generic, ex: network error
                                    ToastHelper
                                            .MakeShortText("Error posting story");
                                }
                            }

                        }).build();
                feedDialog.show();
            }
        }
    });

1
分享页面或链接。
Bundle params = new Bundle();
params.putString("link", "link_url");


Request request = new Request(Session.getActiveSession(), "me/feed", params, HttpMethod.POST);
request.setCallback(new Request.Callback() {
    @Override
    public void onCompleted(Response response) {
        if (response.getError() == null) {
            // Tell the user success!
        }
    }
});
request.executeAsync();

更多的帖子参数请参见developer.facebook.com上的me/feed


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接