Android/Java -- Post Simple Text to Facebook Wall

Android/Java -- Post simple text to Facebook wall?

I figured it out, with Tom's help (thanks). The key was creating a dialog with the "stream.publish" API call, using the Facebook Android SDK. Here are the steps:

  1. Download the official Facebook Android SDK : http://github.com/facebook/facebook-android-sdk
  2. Import the project files into Eclipse.
  3. Export the project as a *.jar file. (this might cause a conflict)

    [UPDATE]

    Facebook recently updated the source code and I noticed the icon file caused resource id conflicts with my projects (Android 1.5+). My solution is to forget about exporting as a jar. Instead, copy the Facebook "com" folder directly into your app's "src" folder (i.e. "com.facebook.android" should be a package in your app... right alongside your source files). If you already have a "com" folder in your "src" folder, don't worry about any dialog boxes that appear about overwriting files, none of your source files should be overwritten. Go back into Eclipse, and refresh the "src" folder and "com.facebook.android" should now be listed as a package. Copy one of the included Facebook icons to your app's "drawable" folder and refresh that as well. Eclipse will complain about the "FbDialog.java" file... just add an import pointing to your app's "R" file to the header of that file (e.g. if your app's package name is "com.android.myapp," then add this: "import com.android.myapp.R;"). Go to #5 if you needed to do this.

  4. Add the .jar file to your project's build path

  5. Look at the following simplified example code:


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import com.facebook.android.*;
import com.facebook.android.Facebook.DialogListener;

public class FacebookActivity extends Activity implements DialogListener,
OnClickListener
{

private Facebook facebookClient;
private LinearLayout facebookButton;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.test);//my layout xml

facebookButton = (LinearLayout)this.findViewById(R.id.Test_Facebook_Layout);

}

@Override
public void onComplete(Bundle values)
{

if (values.isEmpty())
{
//"skip" clicked ?
return;
}

// if facebookClient.authorize(...) was successful, this runs
// this also runs after successful post
// after posting, "post_id" is added to the values bundle
// I use that to differentiate between a call from
// faceBook.authorize(...) and a call from a successful post
// is there a better way of doing this?
if (!values.containsKey("post_id"))
{
try
{
Bundle parameters = new Bundle();
parameters.putString("message", "this is a test");// the message to post to the wall
facebookClient.dialog(this, "stream.publish", parameters, this);// "stream.publish" is an API call
}
catch (Exception e)
{
// TODO: handle exception
System.out.println(e.getMessage());
}
}
}

@Override
public void onError(DialogError e)
{
System.out.println("Error: " + e.getMessage());
}

@Override
public void onFacebookError(FacebookError e)
{
System.out.println("Error: " + e.getMessage());
}

@Override
public void onCancel()
{
}

@Override
public void onClick(View v)
{
if (v == facebookButton)
{
facebookClient = new Facebook();
// replace APP_API_ID with your own
facebookClient.authorize(this, APP_API_ID,
new String[] {"publish_stream", "read_stream", "offline_access"}, this);
}
}
}

Facebook wall post with text and image

you can post your image with text from your application in a very simply way.

Call this method while clicking on the button widget say btnImagePostToWall like...

btnImagePostToWall.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

postImageToWall();
}
});

Get Profile information by making request to Facebook Graph API....

public void postImageToWall() {

facebook.authorize(
this,
new String[] { "user_photos,publish_checkins,publish_actions,publish_stream" },
new DialogListener() {

@Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}

@Override
public void onError(DialogError dialogError) {
// TODO Auto-generated method stub
}

@Override
public void onComplete(Bundle values) {
postImageonWall();
}

@Override
public void onCancel() {
// TODO Auto-generated method stub
}
});
}

private void postImageonWall() {
byte[] data = null;

Bitmap bi = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
Bundle params = new Bundle();
params.putString(Facebook.TOKEN, facebook.getAccessToken());
params.putString("method", "photos.upload");
params.putByteArray("picture", data); // image to post
params.putString("caption", "My text on wall with Image "); // text to post
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request(null, params, "POST", new SampleUploadListener(),
null);
}

Just create a class SampleUploadListener which implements AsyncFacebookRunner.RequestListener...

class SampleUploadListener implements AsyncFacebookRunner.RequestListener {

@Override
public void onComplete(String response, Object state) {
}

@Override
public void onIOException(IOException e, Object state) {
}

@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}

@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}

@Override
public void onFacebookError(FacebookError e, Object state) {
}

}

Hope this will help you a bit.... :-)

post a message to facebook wall from android studio project

post some predefined text

That part is impossible and not allowed, because:

Don't prefill captions, comments, messages, or the user message
parameter of posts with content a person didn’t create, even if the
person can edit or remove the content before sharing.

Source: https://developers.facebook.com/policy/

Obviously, you will never get the required permission (publish_actions) approved for posting predefined text.

How post to wall facebook android-sdk:4.0.0

Maybe it's not quite the solution you are looking for, but I'm using it.

Facebook Android SDK 4 has class ShareApi for sharing your content. This class has static method share():

public static void share(
final ShareContent shareContent,
final FacebookCallback<Sharer.Result> callback) {
new ShareApi(shareContent)
.share(callback);
}

and non static private String message. So when you're trying to share something (for ex.

ShareApi api = new ShareApi(content);
api.setMessage("My message");
api.share(content, new FacebookCallback<Sharer.Result>() ...)

) will be created new instance of ShareApi with message = null and your message won't be added.

The solution:

  1. Open class ShareApi if you're using Facebook SDK as external library OR copy this class from Github https://github.com/facebook/facebook-android-sdk/blob/master/facebook/src/com/facebook/share/ShareApi.java
    if you're using Maven repository.

  2. Change this code:

    public static void share(
    final ShareContent shareContent,
    final FacebookCallback<Sharer.Result> callback) {
    new ShareApi(shareContent)
    .share(callback);
    }

    to this one:

    public static void share(final String message,
    final ShareContent shareContent,
    final FacebookCallback<Sharer.Result> callback) {
    new ShareApi(message, shareContent)
    .share(callback);
    }
  3. Change this code:

    public ShareApi(final ShareContent shareContent) {
    this.shareContent = shareContent;
    this.graphNode = DEFAULT_GRAPH_NODE;
    }

    to this one:

    public ShareApi(String message, final ShareContent shareContent) {
    this.message = message;
    this.shareContent = shareContent;
    this.graphNode = DEFAULT_GRAPH_NODE;
    }
  4. Use your changed ShareApi class for sharing your content:

    ShareApi.share("My message", content, new FacebookCallback<Sharer.Result>() {

    @Override
    public void onSuccess(Sharer.Result result) {
    if (AppConfig.DEBUG) {
    Log.d(TAG, "SUCCESS");
    }
    }

    @Override
    public void onCancel() {
    if (AppConfig.DEBUG) {
    Log.d(TAG, "CANCELLED");
    }
    }

    @Override
    public void onError(FacebookException error) {
    if (AppConfig.DEBUG) {
    Log.d(TAG, error.toString());
    }
    }
    });

If you just want to share text, you can use code below for content object:

ShareLinkContent content = new ShareLinkContent.Builder()
.build();

You've already read this manual https://developers.facebook.com/docs/sharing/android and can add different ShareContent to your post. Use examples from Facebook Github repository for better understanding new SDK.

P.S. Of course, you should have valid access token and publish_actions permission.

How post to wall facebook android-sdk:4.0.0

Maybe it's not quite the solution you are looking for, but I'm using it.

Facebook Android SDK 4 has class ShareApi for sharing your content. This class has static method share():

public static void share(
final ShareContent shareContent,
final FacebookCallback<Sharer.Result> callback) {
new ShareApi(shareContent)
.share(callback);
}

and non static private String message. So when you're trying to share something (for ex.

ShareApi api = new ShareApi(content);
api.setMessage("My message");
api.share(content, new FacebookCallback<Sharer.Result>() ...)

) will be created new instance of ShareApi with message = null and your message won't be added.

The solution:

  1. Open class ShareApi if you're using Facebook SDK as external library OR copy this class from Github https://github.com/facebook/facebook-android-sdk/blob/master/facebook/src/com/facebook/share/ShareApi.java
    if you're using Maven repository.

  2. Change this code:

    public static void share(
    final ShareContent shareContent,
    final FacebookCallback<Sharer.Result> callback) {
    new ShareApi(shareContent)
    .share(callback);
    }

    to this one:

    public static void share(final String message,
    final ShareContent shareContent,
    final FacebookCallback<Sharer.Result> callback) {
    new ShareApi(message, shareContent)
    .share(callback);
    }
  3. Change this code:

    public ShareApi(final ShareContent shareContent) {
    this.shareContent = shareContent;
    this.graphNode = DEFAULT_GRAPH_NODE;
    }

    to this one:

    public ShareApi(String message, final ShareContent shareContent) {
    this.message = message;
    this.shareContent = shareContent;
    this.graphNode = DEFAULT_GRAPH_NODE;
    }
  4. Use your changed ShareApi class for sharing your content:

    ShareApi.share("My message", content, new FacebookCallback<Sharer.Result>() {

    @Override
    public void onSuccess(Sharer.Result result) {
    if (AppConfig.DEBUG) {
    Log.d(TAG, "SUCCESS");
    }
    }

    @Override
    public void onCancel() {
    if (AppConfig.DEBUG) {
    Log.d(TAG, "CANCELLED");
    }
    }

    @Override
    public void onError(FacebookException error) {
    if (AppConfig.DEBUG) {
    Log.d(TAG, error.toString());
    }
    }
    });

If you just want to share text, you can use code below for content object:

ShareLinkContent content = new ShareLinkContent.Builder()
.build();

You've already read this manual https://developers.facebook.com/docs/sharing/android and can add different ShareContent to your post. Use examples from Facebook Github repository for better understanding new SDK.

P.S. Of course, you should have valid access token and publish_actions permission.

How to submit Photos with text together to Facebook wall from Android?

In the documentation of the User object, it says under the photos connection:

You can post photos to a user's Wall on their behalf by issuing an
HTTP POST request to PROFILE_ID/photos with the publish_stream
permissions and the following parameters.

The android fb sdk will do just that for you, it should look something like this:

// facebook being either Facebook or AsyncFacebookRunner

Bundle parameters = new Bundle();
parameters.putString("message", "MESSAGE TO GO WITH THE IMAGE");
parameters.putByteArray("source", imageBytes);

facebook.request("me/photos", parameters, "POST");

I haven't tested it, and kind of "glued" it together after reading the documentation, the source and some threads (Async API Requests, API Requests, Facebook.java, Util.java, Android post picture to Facebook wall). In the last link in the code it says that the parameter name is picture instead of the source that I used, but that's how it states in the documentation, check it out and see which one works.

Also, you can use USER-ID/photoes instead of the me/photos graph object path, just make sure that you have the publish_stream permission.

Next time you ask a question, try to be more specific, maybe add the code you have so far?



Related Topics



Leave a reply



Submit