Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

android: send a facebook post with custom actions

I want to send a facebook post with a custom action which is a hyper link to appear beside the like and comment links.

how can this be done ?

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I prefer copying the Facebook SDK source code to my own Eclipse Android project to simplify debugging. My code is based on the Simple example which comes with the Facebook SDK. So in case you want to copy my code, make sure to add some Classes from the example project as well!

I prefer using JSONObject to create my post which helps me to keep my code clean. Before the post is really sent to the user's wall, I popup a dialog which enables the user to see what will end up on his wall. The user is also able to add his own message if he likes.


    private void publishPhoto(String imageURL) {
        Log.d("FACEBOOK", "Post to Facebook!");

        try {

            JSONObject attachment = new JSONObject();
            attachment.put("message", Utils.s(R.string.fb_message));
            attachment.put("name", Utils.s(R.string.fb_name));
            attachment.put("href", Utils.s(R.string.url_dotzmag));
            attachment.put("description", Utils.s(R.string.fb_description));

            JSONObject media = new JSONObject();
            media.put("type", "image");
            media.put("src", imageURL);
            media.put("href", Utils.s(R.string.url_dotzmag));
            attachment.put("media", new JSONArray().put(media));

            JSONObject properties = new JSONObject();

            JSONObject prop1 = new JSONObject();
            prop1.put("text", "Dotz App on Android Market");
            prop1.put("href", Utils.s(R.string.url_android_market));
            properties.put("Get the App for free", prop1);

            JSONObject prop2 = new JSONObject();
            prop2.put("text", "Dotz Tuning on Facebook");
            prop2.put("href", Utils.s(R.string.url_facebook_fanpage));
            properties.put("Visit our fanpage", prop2);

            attachment.put("properties", properties);

            Log.d("FACEBOOK", attachment.toString());

            Bundle params = new Bundle();
            params.putString("attachment", attachment.toString());
            mFacebook.dialog(mActivity, "stream.publish", params, new PostPhotoDialogListener());
            //mAsyncRunner.request("me/feed", params, "POST", new WallPostRequestListener(), null);

        } catch (JSONException e) {
            Log.e("FACEBOOK", e.getLocalizedMessage(), e);
        }
    }

    public class PostPhotoDialogListener extends BaseDialogListener {

        public void onComplete(Bundle values) {
            final String postId = values.getString("post_id");
            if (postId != null) {
                Log.d("FACEBOOK", "Dialog Success! post_id=" + postId);
                Toast.makeText(mActivity, "Successfully shared on Facebook!", Toast.LENGTH_LONG).show();
                /*
                mAsyncRunner.request(postId, new WallPostRequestListener());
                mDeleteButton.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        mAsyncRunner.request(postId, new Bundle(), "DELETE",
                                new WallPostDeleteListener(), null);
                    }
                });
                */
            } else {
                Log.d("FACEBOOK", "No wall post made");
            }
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...