facebook api with android
<p>I am new to facebook api and android.But somehow try  to manage login to my facebook account and retrieve some information of my account i.e. id,first_name,last_name.The sdk(android) which is used on creating this application is sdk(android) level 8 but when i used sdk(android) level >8 application crash and error generate on logcat(networkonmainthreadException).I had done some search and found this is thread problem with sdk level and now i am going for Asynctask but got confused where to put the login code for facebook and what thing will return to mainactivity</p>

<p>My code for sdk level 8 is:-</p>

<pre><code>protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        loginnfetch=(Button) findViewById(R.id.button1);
        loginnfetch.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                facebook=new Facebook(APP_ID);
                restorecredential(facebook);
                if(!facebook.isSessionValid())
                {
                    loginandfetch();
                }
                else
                {
                    fetch();
                }
            }
        });
    }

    protected void fetch() 
    {
        try {
            JSONObject jobj=new JSONObject(facebook.request("me"));
            int id=jobj.getInt("id");
            String fname=jobj.getString("first_name");
            String lname=jobj.getString("last_name");
            //String emailid=jobj.getString("email");
            Toast.makeText(getApplicationContext(), ".."+id+".."+fname+".."+lname, 0).show();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    protected void loginandfetch() 
    {
        facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,new DialogListener() {

            @Override
            public void onFacebookError(FacebookError e) 
            {
                Toast.makeText(getApplicationContext(), "ERROR WHILE LOGIN", 0).show();

            }

            @Override
            public void onError(DialogError e) {
                Toast.makeText(getApplicationContext(), "ERROR WHILE LOGIN", 0).show();


            }

            @Override
            public void onComplete(Bundle values) {
                saveCredentials(facebook);
                fetch();
            }

            @Override
            public void onCancel() {

                Toast.makeText(getApplicationContext(), "ERROR WHILE LOGIN", 0).show();

            }
        });

    }

    protected boolean restorecredential(Facebook facebook2) 
    {
        SharedPreferences sharedPreferences = getApplicationContext()
                .getSharedPreferences(KEY, Context.MODE_PRIVATE);
        facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
        facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
        return facebook.isSessionValid();

    }
    public boolean saveCredentials(Facebook facebook) {
        Editor editor = getApplicationContext().getSharedPreferences(KEY,
                Context.MODE_PRIVATE).edit();
        editor.putString(TOKEN, facebook.getAccessToken());
        editor.putLong(EXPIRES, facebook.getAccessExpires());
        return editor.commit();
    } 
</code></pre>

<p>Please share some code if available or some link
thank you and sorry if something is not correct</p>
