简体   繁体   中英

Sharing a wall post by facebook android sdk

Hi I am new to android programming. I want to share a image with some description on facebook. I have tried every method explain in answers on stackoverflow. My problem is facebook dialog opens but it doesn't have specified bundle parameters. Please help me with this. I have already tried almost 20 different code snippets. Please give me fully functional code.

    @Override
        public void onClick(View v) {
            fb = new Facebook(app_id);
            Bundle par = new Bundle();
            par.putString("name", "Ass");
            fb.dialog(con,"feed",par, new DialogListener(){

                @Override
                public void onCancel() {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onComplete(Bundle arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onError(DialogError arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onFacebookError(FacebookError arg0) {
                    // TODO Auto-generated method stub

                }});


        }
    });

}

Have you already set up the facebook side of the application on the development page? You need to have your app registered on facebook if you are intending to use their api and/or do graph requests or wall posts on behalf of a user authtoken.

You should read this tutorial (check part 5 for fb app registering) and all of the related info around it to really get going with facebook interaction within your app. I know I did and end up creating my own library for log-in short-cuts or graph requests etc... it's not that simple, it will take you some time aswell.

Also;

Are you using the loginButton from the sdk? Like this:

<com.facebook.widget.LoginButton
    android:id="@+id/authButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp"
    />

It automatically handles user login to facebook whether he has the facebook app installed or not and retrieves a session status result (onStatusChange callback). Make sure you handle that right first and that the session is correctly initiated.

Still, posting your log will give us a better idea of what you encountering with.

I have to say, tho, that the facebook api for android is pretty solid so far so you must be doing something wrong for sure.

<<<<<< EDIT: >>>>>>

Ok then, assuming you have the user correctly logged-in (session.getActiveSession() == Session.OPENED I believe), the next step is to make sure you enabled necessary permissions. This example is from the official facebook documentation, try it (execute publishStory() in your app):

private void publishStory() {
Session session = Session.getActiveSession();

if (session != null){

    // Check for publish permissions    
    List<String> permissions = session.getPermissions();
    if (!isSubsetOf(PERMISSIONS, permissions)) {
        pendingPublishReauthorization = true;
        Session.NewPermissionsRequest newPermissionsRequest = new Session
                .NewPermissionsRequest(this, PERMISSIONS);
    session.requestNewPublishPermissions(newPermissionsRequest);
        return;
    }

    Bundle postParams = new Bundle();
    postParams.putString("name", "Facebook SDK for Android");
    postParams.putString("caption", "Build great social apps and get more installs.");
    postParams.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
    postParams.putString("link", "https://developers.facebook.com/android");
    postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");

    Request.Callback callback= new Request.Callback() {
        public void onCompleted(Response response) {
            JSONObject graphResponse = response
                                       .getGraphObject()
                                       .getInnerJSONObject();
            String postId = null;
            try {
                postId = graphResponse.getString("id");
            } catch (JSONException e) {
                Log.i(TAG,
                    "JSON error "+ e.getMessage());
            }
            FacebookRequestError error = response.getError();
            if (error != null) {
                Toast.makeText(getActivity()
                     .getApplicationContext(),
                     error.getErrorMessage(),
                     Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getActivity()
                         .getApplicationContext(), 
                         postId,
                         Toast.LENGTH_LONG).show();
            }
        }
    };

    Request request = new Request(session, "me/feed", postParams, 
                          HttpMethod.POST, callback);

    RequestAsyncTask task = new RequestAsyncTask(request);
    task.execute();
}


private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
    for (String string : subset) {
        if (!superset.contains(string)) {
            return false;
        }
    }
    return true;
}

The code issues a POST to the users feed wall with the graph params specified at the bundle. If session does not have the needed publish permissions, then a permission request will be issued instead. If user granted those permissions, then the RequestAsyncTask should be executed next time you call the method.

As you can see the basic idea is to get the user to log-in with his facebook account (first step), then request necessary permissions for the action, in this case, publish permissions for a wall post (second step), and lastly issue a graph request into "me/feed" with the params needed for the wall post.

In any case, if you still encountering problems please try to debug from your log as it indicates wether the request failed because of invalid session or no permissions etc...

Post your log here in that case , but this should work.

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