How to post on facebook wall using Facebook android SDK, without opening dialog box

I am using facebook sdk and with the help of example given with that SDK i developed a code to login and store access_token into database

i have 2 problems:

1). when i try to fetch access_token from database and pass it to facebook it doesn't allow me to post on wall using that example given with that facebook sdk, why is that so??

2). i gone through that facebook.java code, but what i got is that to post on a wall i got to open a dialogue box as there is no other method to pass my message straight away and post. Please tell me solution towards it or tel me wat should i do when i want to post on a wall without opening a dialogue box

   mPostButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String message = "Post this to my wall";

                Bundle params = new Bundle();             

                params.putString("message", message);

                mAsyncRunner.request("me/feed", params, "POST", new WallPostRequestListener());

            }
        });

 public class WallPostRequestListener extends BaseRequestListener {

        public void onComplete(final String response) {
            Log.d("Facebook-Example", "Got response: " + response);
            String message = "<empty>";
            try {
                JSONObject json = Util.parseJson(response);
                message = json.getString("message");
            } catch (JSONException e) {
                Log.w("Facebook-Example", "JSON Error in response");
            } catch (FacebookError e) {
                Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
            }
            final String text = "Your Wall Post: " + message;
            Example.this.runOnUiThread(new Runnable() {
                public void run() {
                    mText.setText(text);
                }
            });
        }
    }

Whats wrong with above code.. its not posting on wall and giving me Wall Post: empty on phone and emulator both

Thanks in advance..

Answers


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.


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);        
  } 

}


Need Your Help

Why does Python assignment not return a value?

python python-3.x language-design

Why is Python assignment a statement rather than an expression? If it was an expression which returns the value of the right hand side in the assignment, it would have allowed for much less verbose...

"Stuttering" drag when using d3.behavior.drag() and transform

javascript d3.js

I'm trying to use d3.js's d3.behavior.drag() drag event to update my data model (without setting the element position immediately), then run my "draw" function to update all elements based on the u...