Streetboy
Streetboy

Reputation: 4401

AsyncTask and callbacks Android

I aneed to do login with facebok, for that reason i downloaded sdk and created my method.

After i get info that user is logged i need to send this to my web service and do callback to my activity.

Here is the code i have. onComplete i tried to explain what i need: Well basically i need callback to my current activity.

public class DisplayLoginActivity extends FragmentActivity {



    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_login_activity);

        login();
    }

    public void login(){
        facebook = new Facebook(kAppId);

        facebook.authorize(this, new DialogListener() {

            @Override
            public void onComplete(Bundle values) {
                System.out.println("onComplete");
                        JSONObject me = null;
                        me = new JSONObject(facebook.request("me"));                

                        String id = me.getString("id");     

                        //I want here to call other async request and make it call back to DisplayLoginActivity, for that
                        //reason i created callback with interface so i need to give this of my Activity
                        //If i give this so it's DialogListener this. I tried to do this DisplayLoginActivity.this but it crashes
            }


            @Override
            public void onFacebookError(FacebookError error) {
                error.getMessage();

            }

            @Override
            public void onError(DialogError e) {

            }

            @Override
            public void onCancel() {

            }

        });
    }

}

Maybe you have some ideas ? Thanks.

Edit:

04-20 08:34:50.270: E/AndroidRuntime(7642): FATAL EXCEPTION: Thread-10
04-20 08:34:50.270: E/AndroidRuntime(7642): java.lang.ExceptionInInitializerError
04-20 08:34:50.270: E/AndroidRuntime(7642):     at com.my.main.DisplayLoginActivity$1$1.run(DisplayLoginActivity.java:161)
04-20 08:34:50.270: E/AndroidRuntime(7642):     at java.lang.Thread.run(Thread.java:1019)
04-20 08:34:50.270: E/AndroidRuntime(7642): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
04-20 08:34:50.270: E/AndroidRuntime(7642):     at android.os.Handler.<init>(Handler.java:121)
04-20 08:34:50.270: E/AndroidRuntime(7642):     at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
04-20 08:34:50.270: E/AndroidRuntime(7642):     at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
04-20 08:34:50.270: E/AndroidRuntime(7642):     at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
04-20 08:34:50.270: E/AndroidRuntime(7642):     ... 2 more

Upvotes: 1

Views: 772

Answers (1)

shanet
shanet

Reputation: 7324

I'm not familiar with the Facebook API, but have you tried simply calling Looper.prepare() in the function(s) that run on a separate thread (in this case, onComplete())? It should be the first function call in the thread.

Upvotes: 1

Related Questions