我想发送一条带有自定义操作的Facebook帖子,这是一个超级链接,可以显示在相似位置和评论链接旁边。
怎么办呢?
感谢
答案 0 :(得分: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");
}
}
}
答案 1 :(得分:3)
您应该在请求参数中添加“操作”:
JSONStringer actions;
try {
actions = new JSONStringer().object()
.key("name").value("Click me!")
.key("link").value("http://stackoverflow.com/").endObject();
params.putString("actions", actions.toString());
} catch (JSONException e) {
e.printStackTrace();
}
答案 2 :(得分:1)
private void publishPhoto(String imageURL) {
Log.d("FACEBOOK", "Post to Facebook!");
try {
JSONObject attachment = new JSONObject();
attachment.put("message","Type your message to share");
attachment.put("name", "Your Application Name"));
attachment.put("href", "Any hyperLink");
attachment.put("description","Description about Application");
JSONObject media = new JSONObject();
media.put("type", "image");
media.put("src", "URL path of posting image");
media.put("href","Any hyperLink"));
attachment.put("media", new JSONArray().put(media));
JSONObject properties = new JSONObject();
JSONObject prop1 = new JSONObject();
prop1.put("text", "Text or captionText to Post");
prop1.put("href", "Any hyperLink");
properties.put("Get the App for free(or any custom message))", prop1);
// u can make any number of prop object and put on "properties" for ex: //prop2,prop3
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());
} 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();
} else {
Log.d("FACEBOOK", "No wall post made");
}
}
}
答案 3 :(得分:0)
您可以尝试使用此lib与Facebook通信:
我推荐这个,因为你不必处理这个JSON的东西; - )
请参阅此代码:
// Publishing an image to a photo album is easy!
// Just specify the image you'd like to upload and RestFB will handle it from there.
FacebookType publishPhotoResponse = facebookClient.publish("me/photos", FacebookType.class,
BinaryAttachment.with("cat.png", getClass().getResourceAsStream("/cat.png")),
Parameter.with("message", "Test cat"));
out.println("Published photo ID: " + publishPhotoResponse.getId());
// Publishing a video works the same way.
facebookClient.publish("me/videos", FacebookType.class,
BinaryAttachment.with("cat.mov", getClass().getResourceAsStream("/cat.mov")),
Parameter.with("message", "Test cat"));