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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:05:30+00:00 2026-06-13T10:05:30+00:00

Hi i’m working on facebook api. i have gone through facebook documentation and coded

  • 0

Hi i’m working on facebook api. i have gone through facebook documentation and coded as instructed in it.The below code is working fine in android 2.1-3.2,But while running in android 4.0.4 its giving an error android.os.networkonmainthread. I’m not getting any idea how to write below code using AsynckTask or threads(Painless thread).Please anyone here Suggest me how to code so that it can execute on all versiosn of android.Here is my code.

public class Facebook4 extends Activity {

    public static final String mAPP_ID = "222299217893177";

    public  Facebook mFacebook = new Facebook(mAPP_ID);
    private static final String FACEBOOK_PERMISSION = "publish_stream";
    private SharedPreferences mPrefs;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_facebook4);
        ((ImageView)findViewById(R.id.facebookLogin)).setOnClickListener( loginButtonListener );
        SessionStore.restore(mFacebook, this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        mFacebook.authorizeCallback(requestCode, resultCode, data);
    }

    private OnClickListener loginButtonListener = new OnClickListener() {
        public void onClick( View v ) {
            if( !mFacebook.isSessionValid() ) {
                Toast.makeText(Facebook4.this, "Authorizing", Toast.LENGTH_SHORT).show();
                mFacebook.authorize(Facebook4.this, new String[] {FACEBOOK_PERMISSION},Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
                //startActivity(new Intent(FacebookLogin.this,MainActivity.class));
            }
            else {
                Toast.makeText( Facebook4.this, "Has valid session", Toast.LENGTH_SHORT).show();
                try {
                    JSONObject json = Util.parseJson(mFacebook.request("me"));
                    String facebookID = json.getString("id");
                    String firstName = json.getString("first_name");
                    String lastName = json.getString("last_name");                  
                    Toast.makeText(Facebook4.this, "You already have a valid session, " + firstName + " " + lastName + ". No need to re-authorize.", Toast.LENGTH_SHORT).show();
            }
            catch( Exception error ) {
                Toast.makeText( Facebook4.this, error.toString(), Toast.LENGTH_SHORT).show();
                }
            }
        //startActivity(new Intent(FacebookLogin.this,MainActivity.class));
        }
    };

    public final class LoginDialogListener implements DialogListener {
        public void onComplete(Bundle values) {
        try {
        //The user has logged in, so now you can query and use their Facebook info
        JSONObject json = Util.parseJson(mFacebook.request("me"));
        Log.i("id",  json.getString("id"));
        Log.i("FirstName", json.getString("first_name"));
        Log.i("lastname", json.getString("last_name"));
        Log.i("email", json.getString("email"));
        Log.i("Name", json.getString("name"));
        Log.i("gender",json.getString("gender"));

        SessionStore.save(mFacebook, Facebook4.this);

        }
        catch( Exception error ) {
        Toast.makeText( Facebook4.this, error.toString(), Toast.LENGTH_SHORT).show();
        Log.i("error", error.toString());
        }
        }

        public void onFacebookError(FacebookError error) {
            Toast.makeText( Facebook4.this, "Something went wrong. Please try again.", Toast.LENGTH_LONG).show();
            }
            public void onError(DialogError error) {
            Toast.makeText( Facebook4.this, "Something went wrong. Please try again.", Toast.LENGTH_LONG).show();
            }
            public void onCancel() {
            Toast.makeText( Facebook4.this, "Something went wrong. Please try again.", Toast.LENGTH_LONG).show();
            }
    }   
  • 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-13T10:05:32+00:00Added an answer on June 13, 2026 at 10:05 am

    You are getting this error because you’re doing a network operation, like accessing Facebook on your main UI Thread.

    To fix it in the best way, move all network related code into a separate Thread or AsyncTask

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.