Arslan
Arslan

Reputation:

Integrating Facebook into my Android app

I searched a lot for a perfect example of code that helps me understand how to integrate Facebook in my application. How can I integrate it?

Upvotes: 1

Views: 1792

Answers (3)

Ashish Jani
Ashish Jani

Reputation: 295

private static final String FB_KEY = "YOUR_KEY";
private Facebook facebook;
private String messageToPost;
facebook = new Facebook(FB_KEY);

if (!facebook.isSessionValid()) {
        loginAndPostToWall();
    } else {
        postToWall(messageToPost);
    }

public void loginAndPostToWall() {
    facebook.authorize(activity, FB_PERMISSIONS,
            Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}

public void postToWall(String message) {
    FBThread fbPost = new FBThread (message);
    fbPost.start();
}


  private class FBThread extends Thread {

    String message;

    FBThread(String message) {
        this.message = message;
    }

    @Override
    public void run() {

        Bundle parameters = new Bundle();
        parameters.putString("message", message);

            try {
            facebook.request("me");
            String response = facebook.request("me/feed", parameters,
                    "POST");

            if (response == null || response.equals("")
                    || response.equals("false")) {
                toastMessage = "Blank response.";
            } else if (response.contains("error")) {
                toastMessage = "Post Failed because of duplicates...";
            } else {
                toastMessage = "Message posted to your facebook wall!";
            }

        } catch (Exception e) {
            toastMessage = "Failed to post to wall!";
            e.printStackTrace();
        }

    }
}

    class LoginDialogListener implements DialogListener {
    public void onCancel() {

        android.webkit.CookieManager.getInstance().removeAllCookie();
    }

    public void onComplete(Bundle values) {

        if (messageToPost != null) {
            postToWall(messageToPost);
        }
    }

    public void onError(DialogError error) {

        android.webkit.CookieManager.getInstance().removeAllCookie();
    }

    public void onFacebookError(FacebookError error) {

        android.webkit.CookieManager.getInstance().removeAllCookie();
    }
}

Upvotes: 0

Ramz
Ramz

Reputation: 7164

please do the steps give in the below link facebook android integration

Upvotes: 2

Marvin Rabe
Marvin Rabe

Reputation: 4241

You could use: http://code.google.com/p/facebook4j/

Upvotes: 1

Related Questions