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

  • SEARCH
  • Home
  • 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 8602857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:11:23+00:00 2026-06-12T02:11:23+00:00

I am trying to make a dummy app to play around with Facebook’s Android

  • 0

I am trying to make a dummy app to play around with Facebook’s Android SDK. I currently have SSO and can stored the authentication token with the following code. Essentially it just signs on and then is supposed to make a call to get the userId and then another call when the test button is clicked to print it on the screen.

The authentication token prints fine, but I keep getting a Facebook error thrown when I try to make the call to the graph. What is odd, is that when I copy the request from the log and post it in a browser, I get a valid json response.

I know that it is not ideal to call directly from Facebook (an async request might be better for a polished app) but at the moment, I am just trying to get the basic api working.

Any suggestions?

//Standard Android imports
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;
//Facebook Imports


public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";

    //Something to test and increment to know things are changing
    private static int changeCounter;

    //Create a new Facebook object with the Facebook App ID (found on the facebook app online)
    public String APP_ID;
    Facebook facebook;
    private SharedPreferences mPrefs;

    //For Facebook Async tasks
    private AsyncFacebookRunner mAsyncRunner; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        APP_ID = getString(R.string.APP_ID);
        facebook = new Facebook(APP_ID);
        mAsyncRunner = new AsyncFacebookRunner(facebook);

        changeCounter = 0;

        //Add a button
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //Perform action on click
                Log.v(TAG, "logout button clicked!");

                logoutOfFacebook();
            }
        });

        Button testButton = (Button) findViewById(R.id.button2);
        testButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v(TAG, "testButton clicked");
                testViewPrint();
            }
        });

       checkFBAccessToken(); 
    }

    //Checks if there is an access token in the private mPrefs file and renews
    //the token if it is non existent or out of date
    private void checkFBAccessToken() {
        /* Get existing access_token if any */
        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);
        }

        /* Only call authorize if the access_token has expired */
        if (!facebook.isSessionValid()) {
            renewFBAccessToken();
        }
    }

    //Renews the Facebook access Token and stores in the MODE_PRIVATE SharedPreferences
    private void renewFBAccessToken() {
        //Facebook permissions
        String[] permissions = { "user_location", "friends_location", "email" };

        facebook.authorize(this, permissions, new DialogListener() {
            //@Override
            public void onComplete(Bundle values) {
                SharedPreferences.Editor editor = mPrefs.edit();
                //Get the access token
                editor.putString("access_token", facebook.getAccessToken());
                editor.putLong("access_expires", facebook.getAccessExpires());

                editor.commit();

                //Test the user information recieved
                printUIDAndAuthToken();
            }

            //@Override
            public void onFacebookError(FacebookError error) {}

            public void onError(DialogError e) {
                Log.e(TAG, "There was an error logging out: " + e);
            }

            public void onCancel() {
                Log.d(TAG, "onCancel was hit");
            }

        });

    }

    //Prints the UID and AuthToken to the text view for testing purposes
    private void printUIDAndAuthToken() {
        TextView printOutView = (TextView)findViewById(R.id.textView2);
        String authTokenStr = mPrefs.getString("access_token", "Did not get any AuthToken.");


        String uIdStr = mPrefs.getString("user_id", "Did not get any UID");
        printOutView.setText(changeCounter++ + uIdStr + " | \n" + authTokenStr);
    }

    private void getFBUserId() {
        //Also try to get and store the UID
        SharedPreferences.Editor editor = mPrefs.edit();
        try {
            Bundle params = new Bundle();
            params.putString("acess_token", facebook.getAccessToken());
            editor.putLong("access_expires", facebook.getAccessExpires());

            Log.v(TAG, facebook.getAccessToken() + " ||| =>");
            String jsonStringResponse = facebook.request("me", params);
            Log.v(TAG, jsonStringResponse);
            JSONObject json = new JSONObject(jsonStringResponse);
            String userId;
            userId = json.getString("id");
            editor.putString("user_id", userId);

        } catch (MalformedURLException m) {
            Log.e(TAG, "MalformedURLException");
            m.printStackTrace();
        } catch (IOException i) {
            Log.e(TAG, "IOException Caught");
            i.printStackTrace();
        } catch (Exception e) {
            //Catch possible exception from request
            Log.e(TAG, "JSON or Facebook Error getting user ID: ");
            //Log.e(TAG, e.getMessage());
        }
        editor.commit();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        facebook.authorizeCallback(requestCode, resultCode, data);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    //Prints out a bunch of testing info in the view
    private void testViewPrint() {
        //Testing
        getFBUserId();
        printUIDAndAuthToken();
    }

    //Code called by the button to logout of Facebook
    private void logoutOfFacebook() {

        mAsyncRunner.logout(this, new RequestListener() {
            @Override
            public void onComplete(String response, Object state) {}

            @Override
            public void onIOException(IOException e, Object state) {}

            @Override
            public void onFileNotFoundException(FileNotFoundException e, Object state) {}

            @Override
            public void onMalformedURLException(MalformedURLException e, Object state) {}

            public void onFacebookError(FacebookError e, Object state) {}
        });
    }
}
  • 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-06-12T02:11:24+00:00Added an answer on June 12, 2026 at 2:11 am

    Here is the answer for any beginner Android developers that find themselves with the same error when working with the Facebook SDK.

    It turns out that recent Android releases require you to make all network calls on a thread separate of the main thread (through AysncTask or your own Java threads). If you do try to make a network call on the main thread, instead of hanging the UI, it will simply throw an error as I experienced.

    • 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 a quick dummy app so I can learn the ins
I'm trying to make sense of how cluetip actually works. I have a dummy
I am fairly new to iOS development and trying make a simple app which
Trying to make a small countdown timer in my app but it's not working.
I'm trying to make a function using ddply inside of it. However I can't
I am trying to make a Content Slider for my site. I have multiple
I am just playing around to understand smart pointers and trying to make mine
I'm trying to make some changes to a questionnaire. Currently on each page there
I am trying to make a slideshow-like application in Java using NetBeans. I have
i'm new in android programming, i'm trying make dynamic depended spinner by pass a

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.