Skip to content Skip to sidebar Skip to footer

How To Get User Details After Successful Login Through Facebook

I tried this: when isSessionValid getDetails directly else facebook.authorize and then getDetails in onActivityResult public class MainActivity extends Activity { Facebook fac

Solution 1:

Here in initFacebook() function through you can login and perform you functionality, here i am fetching user's friends information.

privatevoidinitFacebook() 
{
    try
    {
        if (APP_ID == null) 
        {
            Util.showAlert(this,"Warning","Facebook Applicaton ID must be "+ "specified before running this example: see Example.java");
        }

        mFacebook = newFacebook();
        mAsyncRunner = newAsyncFacebookRunner(mFacebook);
        mFacebook.authorize(FacebookList.this, APP_ID, newString[] {"email", "read_stream", "user_hometown", "user_location","friends_about_me", "friends_hometown", "friends_location","user_relationships", "friends_relationship_details","friends_birthday", "friends_education_history","friends_website" }, newDialogListener()
        {
            publicvoidonComplete(Bundle values) 
            {
                getHTTPConnection();
            }

            publicvoidonFacebookError(FacebookError error) 
            {
                Log.i("public void onFacebookError(FacebookError error)....","....");
            }

            publicvoidonError(DialogError e) 
            {
                Log.i("public void onError(DialogError e)....", "....");
                CustomConfirmOkDialog dialog = newCustomConfirmOkDialog(FacebookList.this, R.style.CustomDialogTheme, Utils.FACEBOOK_CONNECTION_ERROR);
                dialog.show();
            }

            publicvoidonCancel() 
            {
                Log.i("public void onCancel()....", "....");
            }
        });

        SessionStore.restore(mFacebook, this);
        SessionEvents.addAuthListener(newSampleAuthListener());
        SessionEvents.addLogoutListener(newSampleLogoutListener());
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

Here in getHTTPConnection(), proceeding for connection and sending fields, that we require about user's friends as here we can see that passing fields are fields=id,first_name,last_name,location,picture of friends. here you can change this fields according to application's requirements.

privatevoidgetHTTPConnection() 
{
    try
    {
        mAccessToken = mFacebook.getAccessToken();
        HttpClienthttpclient=newDefaultHttpClient();
        Stringresult=null;
        HttpGethttpget=newHttpGet("https://graph.facebook.com/me/friends?access_token="+ mAccessToken + "&fields=id,first_name,last_name,location,picture");
        HttpResponse response;
        response = httpclient.execute(httpget);
        HttpEntityentity= response.getEntity();
        if (entity != null) 
        {
            result = EntityUtils.toString(entity);
            parseJSON(result);
        }
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

Now after successfully connecting with facebook , we are getting JSON data and further to parse it .

privatevoidparseJSON(String data1) throws Exception,NullPointerException, JSONException 
{
    try 
    {
        JSONObject jObj = newJSONObject(data1);
        JSONArray jObjArr = jObj.optJSONArray("data");
        int lon = jObjArr.length();


        for (int i = 0; i < lon; i++) 
        {
            JSONObject tmp = jObjArr.optJSONObject(i);

            String temp_image =  tmp.getString("picture");                          String temp_fname = tmp.getString("first_name");

            String temp_lname = tmp.getString("last_name");

            String temp_loc = null;

            JSONObject loc = tmp.getJSONObject("location");
            temp_loc = loc.getString("name");


        }
    } 
    catch (Exception e) 
    {
        Log.i("Exception1 is Here>> ", e.toString());
        e.printStackTrace();
    }
}

It is assumed that you have already added a facebook jar in to your application and for proceeding this code you can call initFacebook() in to onCreate() of your activity

Post a Comment for "How To Get User Details After Successful Login Through Facebook"