I am trying to upload pictures on the wall of Facebook using Android platform.
I downloaded the Facebook SDK for Android, and I am working with this. Afortunally I found a several codes to do it, but didn´t works, some of them have AsyncFacebookRunner but I have not this class on the FB SDK, Do you know when I can use a simple code to do it? with a project?
Thanks,
Andrés
I have added some code to
private final class LoginDialogListener implements DialogListener {
/* and the below code to add image */
JSONObject attachment = new JSONObject();
JSONObject media = new JSONObject();
media.put("type", "image");
media.put("src", image); // where 'image' is the image url or path stored in your device
attachment.put("media", new JSONArray().put(media));
Bundle params = new Bundle();
params.putString("attachment", attachment.toString());
mFacebook.dialog(mActivity, "stream.publish", params,
new SampleDialogListener());
}
add this too
public class SampleDialogListener extends BaseDialogListener {
public void onComplete(Bundle values) {
final String postId = values.getString("post_id");
if (postId != null) {
Log.d("Facebook-Example", "Dialog Success! post_id=" + postId);
mAsyncRunner.request(postId, new WallPostRequestListener());
} else {
Log.d("Facebook-Example", "No wall post made");
}
}
Use facebook developer site fore more information.Hackbook.java file has already explained it very well. thisHackbook.java
Related
i am having an app using FB android SDK v 4.5 where i want to upload image to facebook custom story directly from device or camera. i have created custom story even enabled user generated photo option in story setting(in object or action) in.
below is code snippet i am try to use for custom story post.
How to attach image to directly to this code from device
String shareTitle = bundle.getString("shareTitle");
String shareUrl = bundle.getString("shareUrl");
Bundle params = new Bundle();
JSONObject myObject = new JSONObject();
try {
myObject.put("og:type", "in_myappnamespace:check_in");
myObject.put("og:title", "Check out what i found on #myapptag "
+ shareTitle);
if (shareUrl != null && !shareUrl.equalsIgnoreCase("")) {
myObject.put("og:url", shareUrl);
}
} catch (JSONException e) {
}
params.putString("check_in", myObject.toString());
params.putString("fb:explicitly_shared", "true");
fbGraphRequest = new GraphRequest(AccessToken.getCurrentAccessToken(),
"me/in_myappnamespace:share", params, HttpMethod.POST);
new Thread() {
public void run() {
final GraphResponse response = fbGraphRequest.executeAndWait();
}}.start();
please help me, how to attach image with this request from android device.
thanks,
In my app I need to share link for promo site via Facebook and twitter, in facebook we got something like "share dialog" or post bundle like
Request request = new Request(Session.getActiveSession(), "me/feed", bundle, HttpMethod.POST,
new Request.Callback(){
#Override
public void onCompleted(Response response) {
...
}
});
but there are no twitter sdk for android(trully I use twitter4j) and no way to post bundle
so how to tweet link via twitter?
If you send text with link like "Here is our link: http://stackoverflow.com" - twitter will understand it and get info from your link and show it - as I understand you need to see your recognized link in twitter feed.
To post link you can make intent like #Adrian Sicaru or create your own custom dialog using asynctask with twitter4j as you mention:
#Override
protected Bundle doInBackground(Bundle... params) {
Bundle args = params[0];
Bundle result = new Bundle();
String paramMessage = args.getString(MESSAGE);
String paramLink = args.getString(LINK);
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey("your ConsumerKey");
builder.setOAuthConsumerSecret("your ConsumerSecret");
String accessToken = "your Token";
String accessTokenSecret = "your Secret";
TwitterFactory factory = new TwitterFactory(builder.build());
mTwitter = factory.getInstance(new AccessToken(accessToken, accessTokenSecret));
try {
StatusUpdate status = new StatusUpdate(paramMessage);
if (paramLink != null) {
status = new StatusUpdate(paramMessage + " " + paramLink);
}
mTwitter.updateStatus(status);
} catch (TwitterException e) {
result.putString(RESULT_ERROR, e.getMessage());
}
return result;
}
You can do it via intent like this :
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
This will open a list with apps already installed that can answer the intent, like gmail, facebook and... twitter.
You can find more information here: http://developer.android.com/training/sharing/send.html
Also, if you want to directly call twitter and don't want to choose, have a look at this answer
Like #TribblerWare answered just write link in tweet and twitter recognized it.
By the way, you can use ASNE library for this - just authorize user and use requestPostLink with bundle as parameter
Bundle postParams = new Bundle();
postParams.putString(SocialNetwork.BUNDLE_LINK, link);
socialNetwork.requestPostLink(postParams, message, postingComplete);
more detailed you can see in tutorial
I am curious if I can get some help with Open Graph since I can't seem to make any sense out of the Facebook API that I have read.
Right now I have setup my Open Graph Application on Facebook. It has been approved. I am trying to submit my "objects" via the bundle params but I am curious how I setup a bundle param object like the following. Where myObject has multiple values associated with it.
Bundle params = new Bundle();
param.putString("myObject", ""); // My object has multiple values
I guess I really need to figure out how you submit something in the Bundle that has multiple properties associated with it. If anyone has any insight on this please help me out.
At first I had tried something like this.
Bundle myObject = new Bundle();
myObject("property1", "property1Value");
myObject("property2", "property2Value");
myObject("property3", "property3Value");
Bundle params = new Bundle();
params.putString("myObject", myObject);
But in hindsight I figured out why this wouldn't work.
Edit 1
Maybe this will shed some light. Keep in mind this is an Open Graph action which is not a part of the Graph API.
//Build recipe
JSONObject recipe = new JSONObject();
recipe.put("type", "myappns:recipe");
recipe.put("recipe_name", "Thai Island");
recipe.put("cook_time", "1hr. 30min.");
//Build cookbook
JSONObject cookbookParams = new JSONObject();
cookbookParams.put("type", "myappns:book");
cookbookParams.put("title", "Hot & Spicy");
cookbookParams.put("description", "This book consists of hot & spicy foods");
cookbookParams.put("recipes", new JSONArray().put(recipe));
Bundle params = new Bundle();
params.putString("cookbook", cookbookParams.toString());
AsyncFacebookRunner request = new AsyncFacebookRunner(facebook);
request.request("me/myappns:used", params, "POST", new addToTimelineListener(), null);
Here is a question though as I have been digging more into the Open Graph system. I believe I need to actually have a website setup somewhere, is this correct? I was lead to believe through the introductory documentation of Open Graph that I could create and use my Facebook application on Android without the need of any website. That is use the Open Graph system, I know I can use the application to post feeds and what not which I have done successfully.
Thanks again!
Edit 2
Dont even worry about replying I understand what my problem was now...I have to have a website somewhere hosting a Facebook application for the posts to link back too. Makes perfect sense, I haven't seen where the documentation was very direct about this...oh well now I know.
I use this code to publish on wall for multiple object properties.
private void publishPhoto(String imageURL) {
Log.d("FACEBOOK", "Post to Facebook!");
try {
JSONObject attachment = new JSONObject();
attachment.put("message",text);
attachment.put("name", "MyGreatAndroidAppTest");
attachment.put("href", "http://stackoverflow.com/users/909317/sunny");
attachment.put("description","Test Test TEst");
JSONObject media = new JSONObject();
media.put("type", "image");
media.put("src", imageURL);
media.put("href",imageURL);
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", imageURL);
properties.put(text, 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());
facebook.dialog(MyProjectActivity.this, "stream.publish", params, new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}
#Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
#Override
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(MyProjectActivity.this, "Successfully shared on Facebook!", Toast.LENGTH_LONG).show();
} else {
Log.d("FACEBOOK", "No wall post made");
}
}
#Override
public void onCancel() {
// TODO Auto-generated method stub
}
});
} catch (JSONException e) {
Log.e("FACEBOOK", e.getLocalizedMessage(), e);
}
}
To see a complete example look at the wishlist example.
A complete example for Android is included. The package includes the files to be uploaded on the server and a readme file that explain how to set up all the stuff on the open graph panel.
I'm new to Facebook API on Android, and basically, what I'm trying to do is creating custom wall post for an application I'm developing.
Like when you listen a Shazam a song and you can share the result with your friends.
I believe I've got to create a custom attachment. Here's my code for setting the attachment:
mPostButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Bundle myParams = new Bundle();
String attachment="{\"name\":\"Gran Turismo 5\"," +
"\"href\":\"http://www.unknown.com/?lang=fr\"," +
"\"caption\":\"Sony Computer Entertainment\",\"description\":" +
"\"Une vidéo proposée par Cedemo.\",\"media\":" +
"[{\"type\":\"image\",\"src\":" +
"\"http://www.unknown.com/prepicture//thumb_title/15/15061_1.jpg\"," +
"\"href\":\"http://www.unknown.com/?lang=fr\"}],\"properties\":" +
"{\"Autre lien\":{\"text\":\"Cedemo\",\"href\":\"http://www.unknown.com\"}}}";
myParams.putString("attachment", URLEncoder.encode(attachment);
mFacebook.dialog(Option.this, "stream.publish",myParams,
new SampleDialogListener());
And then, later on:
public class SampleDialogListener extends BaseDialogListener {
public void onComplete(Bundle values) {
final String postId = values.getString("post_id");
if (postId != null) {
Log.d("Facebook-Example", "Dialog Success! post_id=" + postId);
mAsyncRunner.request(postId,values, new WallPostRequestListener());
} else {
Log.d("Facebook-Example", "No wall post made");
}
}
}
I didn't wrote the attachment String, It's just a test taken from another question made in this forum. Anyway, when I call myAsync.request, my app shows an error message, how am I supposed to pass the attachment to my dialog?
Hope I've been clear enough.
Are you sure you need to set custom parameters? It sounds like you can just want to post a Facebook message directly to the wall: you can do this by simply handing in the message parameter as a string -- you only need all that JSON if you want to attach an image etc. And note on facebook's page it says using this api call won't post a status update that others can see on their feed, it will just appear on their own wall. If you just want to post a message with a link you should just be able to use your mAsyncRunner (once you have your valid Facebook session) using this:
String message = "Post this to my wall";
Bundle parameters = new Bundle();
parameters.putString("message", message);
mAsyncRunner.request("me/feed", parameters, "POST", new WallPostRequestListener());
Also may help if you posted the error/response code you're getting from Facebook.
I am trying to develop a Facebook application for Android. I am unable to integrate the "Add Comment" feature to photos in Facebook Albums. Using the Graph API, I can show the previously made comments on a photo. However, I just can't add new comments to a photo.
Can somebody provide me some helpful advice?
here is simple example on doing that..
// post comment to single photo
Bundle parameters = new Bundle();
String target = "";
target = "<PHOTO_ID>/comments";
parameters.putString("message", "post Comment testing");
mAsyncRunner.request(target, parameters, "POST", new FacebookRequestListener(target+" (post comment)"));
and here is a simple example for the listener (you can get this from examples of facebook-android-sdk too)
public class FacebookRequestListener extends BaseRequestListener {
String caller = "default";
public FacebookRequestListener() {
}
public FacebookRequestListener(String caller) {
this.caller = caller;
}
public void onComplete(final String response) {
try {
Log.d(TAG, "FacebookRequestListener|"+caller+":" + response);
} catch (Exception e) {
Log.w(TAG, "Error:"+e.getMessage());
}
}
}