android:发送带有自定义操作的facebook帖子

Min*_*amy 2 android facebook

我想发送一个带有自定义操作的facebook帖子,这是一个超级链接,可以显示在相似和评论链接旁边.

如何才能做到这一点 ?

谢谢

vin*_*ber 6

我更喜欢将Facebook SDK源代码复制到我自己的Eclipse Android项目中以简化调试.我的代码基于Facebook SDK附带的Simple示例.因此,如果您想复制我的代码,请确保从示例项目中添加一些类!

我更喜欢使用JSONObject来创建我的帖子,这有助于我保持代码清洁.在帖子真正发送到用户的墙上之前,我弹出一个对话框,让用户看到最终会在他的墙上发生什么.如果用户喜欢,用户还可以添加自己的消息.


    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");
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)