Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7278681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:54:28+00:00 2026-05-28T22:54:28+00:00

I am trying to make a class to handle all my interactions with Facebook.

  • 0

I am trying to make a class to handle all my interactions with Facebook. I am passing the the Login activity I created to the FacebookConnector object (the object in question) to store the credentials and etc. Please view the video to see what I am dealing with http://www.youtube.com/watch?v=OkHEy9Mh1hc. Below is the FacebookConnector class with the App Id edited out

package it.stick;

import java.io.IOException;

import java.net.MalformedURLException;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class FacebookConnector {

private static final String APP_ID ="************";
private static final String[] PERMISSIONS = new String[] {"user_likes","read_stream", "user_photos", "email","photo_upload", "offline_access", "publish_actions"};

private static final String TOKEN = "access_token";
private static final String EXPIRES = "expires_in";
private static final String KEY = "facebook-credentials";

private Facebook facebook;

private Activity activity;


/** Saves applications credentials */
public boolean saveCredentials(Facebook facebook){
    Editor editor = activity.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
    editor.putString(TOKEN, facebook.getAccessToken());
    editor.putLong(EXPIRES, facebook.getAccessExpires());
    return editor.commit();
}

public boolean restoreCredentials(Facebook facebook){
    SharedPreferences sharedPreferences = activity.getSharedPreferences(KEY, Context.MODE_PRIVATE);
    facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
    facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
    return facebook.isSessionValid();
}

public FacebookConnector(Activity activity){
    facebook = new Facebook(APP_ID);
    this.activity = activity;
}

// Creates new Facebook session and stores credentials
public void login(){
    // 1.Restores previous credentials 
    //2.Creates and saves new session if previous session is not valid
    restoreCredentials(facebook);
    if(!facebook.isSessionValid()){
        facebook.authorize(activity, PERMISSIONS, new LoginDialogListener());
    }
}
public boolean isSessionValid() {
    return facebook.isSessionValid();
}

public void logout(){
    try {
        facebook.logout(activity.getApplicationContext());
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void postToWall(String message){
    Bundle parameters = new Bundle();
    parameters.putString("message", message);
    parameters.putString("description", "topic share");

    try{
        facebook.request("me");
        String response = facebook.request("me/feed", parameters, "POST");
        Log.d("Tests", "got response: " + response);
        if(response == null || response.equals("") || response.equals("false")){
            showToast("Blank response from facebook.");
        }
        else{
            showToast("Message posted to your facebook wall.");
        }
    }
    catch(Exception e){
        showToast("Failed to post to wall. We fucked up :(");
        e.printStackTrace();
    }       
}

class LoginDialogListener implements DialogListener{

    @Override
    public void onComplete(Bundle values) {
        saveCredentials(facebook);
        postToWall("logged on to Stick.it");            
    }

    @Override
    public void onFacebookError(FacebookError e) {
        showToast("Authentification with Facebook failed!");            
    }

    @Override
    public void onError(DialogError e) {
        showToast("Authentification with Facebook failed!");        
    }

    @Override
    public void onCancel() {
        showToast("Authentification with Facebook failed!");
    }

}

public void showToast(String message){
    Toast.makeText(activity.getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}

}

Here is the log cat:

02-01 22:39:39.459: W/KeyCharacterMap(637): No keyboard for id 0
02-01 22:39:39.470: W/KeyCharacterMap(637): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-01 22:39:46.299: D/dalvikvm(637): GC_EXPLICIT freed 91K, 50% free 2744K/5447K, external 3745K/3903K, paused 1017ms
02-01 22:39:46.999: D/webviewglue(637): nativeDestroy view: 0x303080
02-01 22:39:48.239: D/dalvikvm(637): GC_EXTERNAL_ALLOC freed 54K, 51% free 2706K/5447K, external 3331K/3903K, paused 735ms
02-01 22:39:52.139: D/Facebook-WebView(637): Webview loading URL: https://m.facebook.com/dialog/oauth?display=touch&client_id=228461823905169&scope=user_likes%2Cread_stream%2Cuser_photos%2Cemail%2Cphoto_upload%2Coffline_access%2Cpublish_actions&type=user_agent&redirect_uri=fbconnect%3A%2F%2Fsuccess
02-01 22:40:12.209: D/Facebook-authorize(637): Login failed: com.facebook.android.DialogError: The connection to the server was unsuccessful.
02-01 22:40:12.279: D/Facebook-WebView(637): Webview loading URL: https://m.facebook.com/dialog/oauth?display=touch&client_id=228461823905169&scope=user_likes%2Cread_stream%2Cuser_photos%2Cemail%2Cphoto_upload%2Coffline_access%2Cpublish_actions&type=user_agent&redirect_uri=fbconnect%3A%2F%2Fsuccess
02-01 22:46:14.119: W/KeyCharacterMap(637): No keyboard for id 0
02-01 22:46:14.119: W/KeyCharacterMap(637): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

I have cleared the browser cache and the app data. I have checked the key hash and I have copied and pasted the App ID multiple times into the app.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:54:28+00:00Added an answer on May 28, 2026 at 10:54 pm

    Be sure, you have added your application’s signature to application setting, as:

    http://developers.facebook.com/docs/mobile/android/build/#sig

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make this code generic: public final Object unwrap(Class arg0) { throw
I'm trying to make a class where I put a key and value into
I've been trying to make a generic class to represent a range of values
I was trying to make my own class for currencies using longs, but apparently
I am currently trying to make a navigation-menu where an active-class is applied to
I am trying to make a call to a Parent class method from a
I'm trying to make a module based php system. Each module is a class
I am trying to extend the mysqli class to make a helper class that
I'm trying to extend the DOMDocument class so as to make XPath selections easier.
I'm trying to create a Mockito mock object of a class with some rather

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.