回答
您應該添加「動作」的請求參數:
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();
}
我喜歡抄襲了Facebook SDK源代碼,以我自己的Eclipse的Android項目,簡化調試。我的代碼基於Facebook SDK附帶的簡單示例。所以如果你想複製我的代碼,請確保從示例項目中添加一些類!
我更喜歡使用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");
}
}
}
好的答案..謝謝.. – Venky 2011-09-23 09:17:39
nice.but我不想要對話框我發送這樣的請求res = facebook.request(「我/飼料」,PARAMS,「POST」);我得到{「error」:{「message」:「(#100)Missing message or attachment」,「type」:「OAuthException」,「code」:100}}如何解決它 – 2013-03-19 05:10:44
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");
}
}
}
我見過的最佳代碼。完美的工作。 – user1220332 2012-02-22 06:56:37
你可以試試這個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"));
你也可以添加其他參數:course.- – radzio 2012-02-22 07:07:24
- 1. 如何使用REBOL發送帶有自定義標題的HTTP帖子
- 2. 我可以通過我的臉書個人資料自動在網站上發佈臉書帖子嗎?
- 3. Android:發送沒有迴應的帖子
- 4. 帶有動作按鈕的Android自定義動作欄
- 5. 臉書發送按鈕
- 6. Android HTTP發送不發送帖子
- 7. Android無法發送帖子
- 8. WordPress的:自定義帖子類型,發送數據到「register_meta_box_cb」Arg
- 9. 鏈接到帶有自定義帖子的頁面?
- 10. WordPress的自定義帖子
- 11. WordPress的自定義帖子
- 12. 調用相關作者的自定義帖子帖子內容
- 13. 發送自定義帖子類型字段到多個郵件
- 14. 要顯示具有自定義帖子類型的帖子
- 15. WordPress publish_post鉤子沒有爲自定義帖子類型發射
- 16. 自定義Google表單發帖操作
- 17. 將自定義帖子移動到普通帖子
- 18. Moodle自動發送證書
- 19. 帶有自定義ListAdapter的Android LinearLayout/TableLayout。製作書目預覽的列表項
- 20. 有多個自定義帖子類型
- 21. Facebook自定義帖子
- 22. 自定義帖子「變體」
- 23. 自定義帖子類型
- 24. 自定義帖子類型
- 25. 發送android的帖子請求
- 26. XMLRPC - 帶自定義帖子類型和自定義字段的wp.newPost
- 27. 在wordpress中自定義搜索所有和自定義帖子
- 28. 發送帶有多個(LOTS)參數的帖子查詢
- 29. 發送帶有跨域帖子的憑證?
- 30. 沒有作者的自定義帖子類型
完美的代碼! – Jonno 2012-03-10 05:46:32
嗨我綁你的代碼發佈像{「名稱」:「點擊我!」,「鏈接」:「http://stackoverflow.com/」}你能幫我從這個請... – GoCrazy 2012-07-16 12:19:32
非常感謝,它爲我工作:) – Kunu 2014-08-04 06:04:15