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 8773539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:19:16+00:00 2026-06-13T18:19:16+00:00

I am writing a simple app on android to get the information of user

  • 0

I am writing a simple app on android to get the information of user who login the app throught Facebook. Here is my codes:

public class example extends Activity {

Facebook facebook = new Facebook("id");
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);

private static final String TAG ="Debug";
private SharedPreferences mPrefs;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_example);
    /*
     * 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()) {

        facebook.authorize(this, new String[] {"publish_stream","email","user_groups","read_stream","user_about_me","offline_access"}, new DialogListener() {
            @Override
            public void onComplete(Bundle values) {
                SharedPreferences.Editor editor = mPrefs.edit();
                editor.putString("access_token", facebook.getAccessToken());
                editor.putLong("access_expires", facebook.getAccessExpires());
                editor.commit();
            }

            @Override
            public void onFacebookError(FacebookError error) {}

            @Override
            public void onError(DialogError e) {}

            @Override
            public void onCancel() {}
        });
    }        
    Log.d(TAG, access_token);
    getInformation();
}

private void getInformation(){

    JSONObject json = null;

    try
    {
        Log.d("testing", "point1");
        String response = facebook.request("me");
        Log.d("testing", "point2");
        json = Util.parseJson(response);
        String id = json.getString("id");
        Log.d("testing", id);
    }
    catch (MalformedURLException e)
    {
        e.printStackTrace();
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    catch (FacebookError e)
    {
        e.printStackTrace();
    }

}

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

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

@Override
public void onResume() {    
    super.onResume();
    facebook.extendAccessTokenIfNeeded(this, null);
}
}

I follow here https://developers.facebook.com/docs/mobile/android/build/#sdk and use the same facebook SDK. So as show on the code, in logcat there is only point1 but not point2, which means the facebook.request(“me”) is not success. The emulator shows the app shut down and say “unfortunately, example has stopped.”

So can anyone help me to point out the question? I wondering if the is something wrong in the access token but I dont know about it. Thanks a lot.

LogCat:

11-03 10:13:55.986: D/Debug(893): AAAFi20EvlJEBACwBLIR4buSOaZB1emxACFPZCZAKums6ZCNwZB0wZCvQnI3grshT0YNl8ZC427BSkrNye6akdCZBjvp4KL24Oyx......
11-03 10:13:55.986: D/testing(893): point1
11-03 10:13:56.106: D/AndroidRuntime(893): Shutting down VM
11-03 10:13:56.106: W/dalvikvm(893): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
11-03 10:13:56.136: E/AndroidRuntime(893): FATAL EXCEPTION: main
11-03 10:13:56.136: E/AndroidRuntime(893): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crush/com.example.crush.Crush}: android.os.NetworkOnMainThreadException
11-03 10:13:56.136: E/AndroidRuntime(893):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
  • 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-13T18:19:17+00:00Added an answer on June 13, 2026 at 6:19 pm

    You’re running a network operation on main thread. Use async task to run network operations in background thread (do your http requests in a background thread). That’s why you are getting android.os.NetworkOnMainThreadException.

    Do your networking in an async task like this:

    class FacebookRequestTask extends AsyncTask{
    
    
        protected void onPreExecute() {
        //show a progress dialog to the user or something
        }
    
        protected void doInBackground() {
            //Do your networking here
        }
    
        protected void onPostExecute() {
            //do something with your parsed 
            //response (JSON data in case of facebook API's) here 
            // and dismiss the progress dialog
        }
      }
    
      new facebookRequestTask().execute();
    

    Here are some tutorials for you if you don’t know how to use async tasks:

    http://mobileorchard.com/android-app-developmentthreading-part-2-async-tasks/

    http://www.vogella.com/articles/AndroidPerformance/article.html

    Here are the official docs from Google:

    https://developer.android.com/reference/android/os/AsyncTask.html

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

Sidebar

Related Questions

I'm writing simple Android app to filter some events from user's calendar. The questions
I'm new to android and am writing a simple app that calculates the distance
I'm writing a simple iPhone app which lets a user access a series of
I am writing an android app that is communicating with a webserver to get
I'm writing simple APN toggle app. I wanted to ask how to force android
I am writing a simple android app which have a edittext and a button.
I'm writing an android app just to get familiar with the OS and API.
I'm writing a simple app in Android. I've encountered this problem: I've two EditText
I am writing a simple app to enter a user into database & display
Hello I'm writing a little Android app (Version 2.3.3). Now i get this strange

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.