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
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
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");
}
}
}
You should add "actions" to the request parameters:
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();
}
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");
}
}
}
You can try this lib for communicating with facebook:
I recommend this because you don't have to deal with this JSON stuff ;-)
See this code:
// 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"));