Post On Facebook Wall using Android SDK – Connect API

In this tutorial, I have covered how to Post on Facebook wall using Android SDK. Android is the most people choice on smart phone side and Facebook now became an open social media to share our thoughts. Do you ever tried to join them both, of course Facebook has its own mobile app, apart from this its possible for us to integrate Facebook with our Android smart phone. We can Post,Share, like and we can do many such actions through our code. To make this happen we need a smart phone to test our app and a Facebook developer account. Apart from this Facebook provides FACEBOOK Android SDK to use Facebook APIs in Android.

[download title1=”Post on Facebok Wall Using Android SDK source code” url1=”http://cdn.hayageek.com.s3.amazonaws.com/downloads/android/AndroidSDKPostOnWall.zip”%5D

Download Facebook Android SDK from this link  and Follow the steps to make an app to Post on Facebook wall using Android SDK.

1.Create an APP in Facebook Developers Portal

Goto https://developers.facebook.com/ and  create an app as shown in the below images.

 


2. Add Facebook SDK to Eclipse

Open your eclipse and create a new android project.   Add FACEBOOK SDK as a library on your present project  as show in the below images.

 


 

3). Code changes

3.1) In AndroidManifest.xml add the permission to access internet permission.

<uses-permission android:name="android.permission.INTERNET"/>

 

3.2) Declare variables inside our Activity class

    private Facebook facebook;
    private AsyncFacebookRunner mAsyncRunner;
    String FILENAME = "AndroidSSO_data";
    private SharedPreferences mPrefs;

 

3.3) Add the below lines in OnCreate() method

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        facebook = new Facebook(APP_ID);
        mAsyncRunner = new AsyncFacebookRunner(facebook);
	}

AsyncFacebookRunner class provides the ability to execute API methods and have the call return immediately, without blocking the calling thread. This is necessary when accessing the API in the UI thread, for instance. The request response is returned to the caller via a callback interface, which the developer must implement.

 

3.4)  You can use the below function to show Facebook oauth dialog window.

public void FacebookLoginWindow() {
    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);
 
    if (access_token != null) {
        facebook.setAccessToken(access_token);
    }
 
    if (expires != 0) {
        facebook.setAccessExpires(expires);
    }
 
    if (!facebook.isSessionValid()) {
        facebook.authorize(this,
                new String[] { "email", "publish_stream" },
                new DialogListener() {
 
                    @Override
                    public void onCancel() {
                        // Function to handle cancel event
                    }
 
                    @Override
                    public void onComplete(Bundle values) {
                        // Function to handle complete event
                        // Edit Preferences and update facebook acess_token
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token",
                                facebook.getAccessToken());
                        editor.putLong("access_expires",
                                facebook.getAccessExpires());
                        editor.commit();
                        
                      //We got the token, so we can call postOnMyWall() here.

 
                    }
 
                    @Override
                    public void onError(DialogError error) {
                        // Function to handle error
 
                    }
 
                    @Override
                    public void onFacebookError(FacebookError fberror) {
                        // Function to handle Facebook errors
 
                    }
 
                });
    }
}

 

3.5) You can use the below code to show “Post to Wall” Dialog.

public void postOnMyWall() {

 facebook.dialog(this, "feed", new DialogListener() {
 
        @Override
        public void onFacebookError(FacebookError e) {
        }
 
        @Override
        public void onError(DialogError e) {
        }
 
        @Override
        public void onComplete(Bundle values) {
        }
 
        @Override
        public void onCancel() {
        }
    });
}

You need to call postOnMyWall() inside the method onComplete() of DialogListener class