Post on Facebook wall using Facebook Android SDK without opening dialog box

i applied following code and could successfully posted my message on wall.

public void postOnWall(String msg) {
        Log.d("Tests", "Testing graph API wall post");
         try {
                String response = mFacebook.request("me");
                Bundle parameters = new Bundle();
                parameters.putString("message", msg);
                parameters.putString("description", "test test test");
                response = mFacebook.request("me/feed", parameters, 
                        "POST");
                Log.d("Tests", "got response: " + response);
                if (response == null || response.equals("") || 
                        response.equals("false")) {
                   Log.v("Error", "Blank response");
                }
         } catch(Exception e) {
             e.printStackTrace();
         }
    }

I updated my tutorial at http://www.integratingstuff.com/2010/10/14/integrating-facebook-into-an-android-application/ and it is now exactly answering this question. It is based on and basically the same as Ankit's answer, but guides people from start to finish through implementing the whole process.


Well it's not that something gets posted on wall without informing user. When user allows your application, then the Android Facebook sdk presents another page, where there is a text that your applications sends, and a textBox where user can write on his wall, similar to the screenshot i have attached

The actual layout on mobile device is slightly different, but it's in the same format. This process is well shown in the sample examples of facebook android sdk.

Now check the question asked in this post:

Facebook Connect Android -- using stream.publish @ http://api.facebook.com/restserver.php'>Facebook Connect Android -- using stream.publish @ http://api.facebook.com/restserver.php

In that question look for these : postParams.put(), similar type of lines will be there in some of your JAVA files. These are the lines using which you can post the data to Facebook.

For example:

postParams.put("user_message", "TESTING 123");

is the message,

postParams.put("attachment", "{\"name\":\"Facebook Connect for Android\",\"href\":\"http://code.google.com/p/fbconnect-android/\",\"caption\":\"Caption\",\"description\":\"Description\",\"media\":[{\"type\":\"image\",\"src\":\"http://img40.yfrog.com/img40/5914/iphoneconnectbtn.jpg\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone/\"}],\"properties\":{\"another link\":{\"text\":\"Facebook home page\",\"href\":\"http://www.facebook.com\"}}}");

is the line where you are providing icon for application, description,caption, title etc.

alt text


I used Ankit's code for posting on facebook wall but his code give me error android.os.NetworkOnMainThreadException.

After searching on this problem a solution told me that put your code in AsyncTask to get rid out of this problem. After modified his code it's working fine for me.

The modified code is looks like:

public class UiAsyncTask extends AsyncTask<Void, Void, Void> {

    public void onPreExecute() {
        // On first execute
    }

    public Void doInBackground(Void... unused) {
        // Background Work
         Log.d("Tests", "Testing graph API wall post");
         try {
                String response = facebook.request("me");
                Bundle parameters = new Bundle();
                parameters.putString("message", "This test message for wall post");
                parameters.putString("description", "test test test");
                response = facebook.request("me/feed", parameters, "POST");
                Log.d("Tests", "got response: " + response);
                if (response == null || response.equals("") || response.equals("false")) {
                   Log.v("Error", "Blank response");
                }
         } catch(Exception e) {
             e.printStackTrace();
         }
        return null;
    }

    public void onPostExecute(Void unused) {
         // Result
    }
}

This class helps me for sent messages on my Facebook wall WITHOUT dialog:

public class FBManager{

  private static final String FB_ACCESS_TOKEN = "fb_access_token";
  private static final String FB_EXPIRES = "fb_expires";

  private Activity context;
  private Facebook facebookApi;

  private Runnable successRunnable=new Runnable(){
    @Override
    public void run() {
        Toast.makeText(context, "Success", Toast.LENGTH_LONG).show();
    }
  };    

  public FBManager(Activity context){

    this.context = context;

    facebookApi = new Facebook(FB_API_ID);

    facebookApi.setAccessToken(restoreAccessToken());

  } 

  public void postOnWall(final String text, final String link){
    new Thread(){
        @Override
        public void run(){
            try {
                Bundle parameters = new Bundle();
                parameters.putString("message", text);
                if(link!=null){
                    parameters.putString("link", link);
                }
                String response = facebookApi.request("me/feed", parameters, "POST");
                if(!response.equals("")){
                    if(!response.contains("error")){
                        context.runOnUiThread(successRunnable);                     
                    }else{
                        Log.e("Facebook error:", response);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }.start();     
  }


  public void save(String access_token, long expires){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    Editor editor=prefs.edit();
    editor.putString(FB_ACCESS_TOKEN, access_token);
    editor.putLong(FB_EXPIRES, expires);
    editor.commit();
  }

  public String restoreAccessToken(){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    return prefs.getString(FB_ACCESS_TOKEN, null);      
  }

  public long restoreExpires(){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    return prefs.getLong(FB_EXPIRES, 0);        
  } 

}