androidcodeexamples.com
Offcode ltd logo
ANDROID
APACHE
THUMBNAILS
SCRIPTS
GFX
World-class examples brought to you by Offcode ltd
ANDROID
Developing the empire Bluetooth low energy Facebook API Google URL shortener Short URL lookup Tabs and fragments Privacy policy Front Page
androidcodeexamples.com

Android Facebook programming example

Facebook imports and required classes for the Graph API interface with JSON objects:

import com.facebook.*; 
import com.facebook.login.widget.LoginButton; 
import com.facebook.login.LoginResult; 
 
 
import org.json.JSONException; 
import org.json.JSONObject; 
 


Facebook example to use login button and get the first name

In this simple example we use the Facebook SDK to login and get the first name. First of all, we expect you have registered your app on Facebook and you have the correct IDs set up. The example instructs how to get the first name via the Graph API. There are several examples on the Internet that use deprecated ways. If you happen to use a new Android release, your app suddenly no longer works! Thus, take no shortcuts here but use the Graph API properly.

The updateProfile() is an internal methof of the SelfAd app. Don't worry about it or leave it empty.


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
        view = inflater.inflate(R.layout.first_tab, container, false); 
 
        LoginButton button; 
        button = (LoginButton) view.findViewById(R.id.login_button); 
        button.setReadPermissions("email"); 
 
        cm = CallbackManager.Factory.create(); 
        accessToken = AccessToken.getCurrentAccessToken(); 
        updateProfile(); 
 
        button.registerCallback(cm, 
                new FacebookCallback<LoginResult>() { 
                    @Override 
                    public void onSuccess(LoginResult loginResult) { 
                        accessToken = AccessToken.getCurrentAccessToken(); 
                        setFacebookData(loginResult); 
                    } 
 
                    @Override 
                    public void onCancel() { 
                        Log.d(TAG, "On Cancel"); 
                    } 
 
                    @Override 
                    public void onError(FacebookException exception) { 
                        showAlertDialog(getString(R.string.facebook_error_dialog_title), exception.getMessage()); 
                        Log.e(TAG, exception.getMessage()); 
                    } 
                }); 
 
        accessTokenTracker = new AccessTokenTracker() { 
            @Override 
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) { 
                Log.d(TAG, "Access token changed!"); 
                accessToken = newAccessToken; 
                updateProfile(); 
            } 
        }; 
 
 
        init(); 
        return view; 
    } 
     

Getting the "first_name" via the Graph API

The code below is run when the FacebookCallback() is successfully called with its onSuccess() callback. It simply puts the first name in the string "firstName" as seen below, upon its completion:

private void setFacebookData(final LoginResult loginResult) 
    { 
        GraphRequest request = GraphRequest.newMeRequest( 
                loginResult.getAccessToken(), 
                new GraphRequest.GraphJSONObjectCallback() { 
                    @Override 
                    public void onCompleted(JSONObject object, GraphResponse response) { 
                        if (response.getError() != null) { 
                            Log.d(TAG, "FB error response: " + response.getError().getErrorMessage()); 
                            showAlertDialog(getString(R.string.facebook_error_dialog_title), response.getError().getErrorMessage()); 
                        } else { 
                            try { 
                                firstName = response.getJSONObject().getString("first_name"); 
                                updateProfile(); 
                            } catch (JSONException e) { 
                                e.printStackTrace(); 
                            } 
                        } 
                    } 
                }); 
        Bundle parameters = new Bundle(); 
        parameters.putString("fields", "id,first_name"); 
        request.setParameters(parameters); 
        request.executeAsync(); 
    }