简体   繁体   中英

Share link and text with Android Facebook SDK 3.0

Im trying to upgrade to the Facebook SDK 3.0 and have finally gotten everything to work with Request.newStatusUpdateRequest(). However my app shares/posts text along with a link. I have tried/looked into the following:

Request.newStatusUpdateRequest()

This does not seem to have any options for a Bundle or any other way to include a link and icon.

Request.newRestRequest()

Skipped this because I saw REST was being depreciated.

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

This actually works pretty well but the resulting post does not seem to be linked to my Facebook App and I am not sure how this will effect my Facebook insights.

Request.newPostRequest()

From what I have read, this method seems to be the proper way. However, i cannot figure out where to get the GraphObject to pass in as one of the parameters.

What is the PROPPER way to post/share text, link and image to the user's wall? It seems to be Request.newPostRequest() so I will include the code I have for that.

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);

But what really is a GraphObject? Where do i get the graph_object? The more I read from FB on GraphObject/OpenGraph/Graph API the more I get confused.

If I am heading down the wrong direction entirely, please tell me. If Request.newPostRequest is the propper way of doing this, please give me more information on the GraphObject param.

Finally managed to get everything I needed with the Facebook SDK 3.0 using the following:

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();

I did by using this method. See if this can help or not.

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();
            }
        }
    });

To share page or link

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();

For more post parameters see me/feed on developer.facebook.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM