0

I have created my android code wherein I fetch all the data from facebook and then displays it in a textView and it works just fine on my account. But after I tried to connect with my dummy account, no details is fetched and I don't know the cause of this problem. Well here's my code to review:

private void getFbName() {
    mProgress.setMessage("Finalizing ...");
    mProgress.show();

    new Thread() {
        @Override
        public void run() {
            String name = "";
            int what = 1;

            try {
                String me = mFacebook.request("me");

                JSONObject jsonObj = (JSONObject) new JSONTokener(me).nextValue();
                name = jsonObj.getString("first_name") + "|" + jsonObj.getString("last_name") + "|" + jsonObj.getString("email") + "|" + jsonObj.getString("id");
                what = 0;
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }

            mFbHandler.sendMessage(mFbHandler.obtainMessage(what, name));
        }
    }.start();
}

private Handler mFbHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        mProgress.dismiss();

        if (msg.what == 0) {
            String username = (String) msg.obj;
            username = (username.equals("")) ? "No Name" : username;

            //SPLITTER
            String tokens[] = username.split("\\|");
            TextView fname = (TextView) findViewById(R.id.textView1);
            fname.setText(tokens[0]);

            TextView lname = (TextView) findViewById(R.id.textView2);
            lname.setText(tokens[1]);

            TextView eadd = (TextView) findViewById(R.id.textView3);
            eadd.setText(tokens[2]);

            TextView fbid = (TextView) findViewById(R.id.textView4);
            fbid.setText(tokens[3]);

            SessionStore.saveName(username, Main.this);
            //mFacebookBtn.setText("       Facebook (" + username + ")");

            Toast.makeText(Main.this, "Connected to Facebook as " + username, Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(Main.this, "Connected to Facebook", Toast.LENGTH_SHORT).show();
        }
    }
};

Well I only followed this tutorial for my code with some modification in the design wherein I use 1 button for login and another for logout and then displays the result in 4 textViews.


Edit
Here's my logout code:

private void fbLogout() {
    mProgress.setMessage("Disconnecting from Facebook");
    mProgress.show();

    new Thread() {
        @Override public void run() {
            SessionStore.clear(Main.this);
            int what = 1;
            try {
                mFacebook.logout(Main.this);
                what = 0;
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }

            mHandler.sendMessage(mHandler.obtainMessage(what));
        }
    }.start(); 
}
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
KaHeL
  • 4,301
  • 16
  • 54
  • 78
  • How do you login/logout the user to/from facebook? – Nitzan Tomer Jun 26 '12 at 09:08
  • heres my logout code private void fbLogout() { mProgress.setMessage("Disconnecting from Facebook"); mProgress.show(); new Thread() { @Override public void run() { SessionStore.clear(Main.this); int what = 1; try { mFacebook.logout(Main.this); what = 0; } catch (Exception ex) { ex.printStackTrace(); } mHandler.sendMessage(mHandler.obtainMessage(what)); } }.start(); } – KaHeL Jun 26 '12 at 09:08
  • Code in comments is not readable, I edited your question and added the code there. How do you log the user in? Is it using SSO? – Nitzan Tomer Jun 26 '12 at 09:19
  • oh thanks, well I don't know exactly the process in logging in it use I just use the facebook src from the tutorial in the link I've given then edited the some code in my main class but I didn't touch the facebook classes though. – KaHeL Jun 26 '12 at 10:03

1 Answers1

1

When using the facebook android SDK, you two have two types of authentication:

  1. Using the SDK auth dialog which will ask the user for his email/password.
  2. Using Single Sign-On (SSO) which will only work if the device has the facebook application (katana) installed. If that's the case, that app will be responsible for the authentication which creates a better user experience since the user is already signed in and does not need to reenter his credentials.

If you are following the tutorial then you are using SSO (if the app is installed of course), and because of that when ever you are using the facebook.authorize method you are asking the fb app to authorize your app for the current logged in user.
If you want another user to use your app you'll need the user to log out of the main facebook app.

You can use the sdk authentication and bypass the SSO as suggested here: How to disable Facebook single sign on for android - Facebook-android-sdk, but as I said before, I think it results in a bad user experience.

Another thing is that you keep implementing things using threads, but the facebook android SDK already gives you a helper class for that, it's the AsyncFacebookRunner which makes api requests asynchronously, read Async API Requests.

Community
  • 1
  • 1
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • Tried doing the solution but I think it just got worst. I've changed my code into this: mFacebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, new FbLoginDialogListener()); Reinstalled the apk then run the app and still the info is not displayed. Much worse even my account which runs well on my first work doesn't even display information now. Hmm.. – KaHeL Jun 26 '12 at 10:45
  • I think I need to share my work for you to carefully review it. here's the file http://www.mediafire.com/?jkwqujubp8p4m9q – KaHeL Jun 26 '12 at 10:51
  • Before I get into your code I wanna know at what stage it fails for you. Is an exception thrown? Maybe you should log to see where it fails – Nitzan Tomer Jun 26 '12 at 11:55
  • Yes it's in the part on the exeption throw. I've used logs to set a breakpoints and add it on the try block where it will return a text of 'I've been here' but no return was displayed on my logs. – KaHeL Jun 27 '12 at 02:09
  • Oh, I think I get it. I need to make a try catch in my data request since the account I've been accessing has a null email and therefore mining the data from JSON returns an error since there is no field of email is returned. Will try it again. – KaHeL Jun 27 '12 at 02:58