0

I have been struggling with android and the Facebook's graph API and I'm sure this is a piece of cake for intermediate programmers. What I want is a very simple Android application that logs in a user to Facebook, get the users consent to access information and finally to seamlessly query the graph API to get the users friends. I mention the work seamless because I have seen many sample applications that require the user to push a "get friends" button but I do not want that. I wish to extract the users friends without requiring him to push anything.

I have pasted the code below. I am able to to log-in and grant permission to my application. Next, I expect it to display my friends but it just directs me to a blank page. The problem might be that I am accessing the graph API in the wrong place because the Logcat does not contain the messages I print before and after the request. Could you please help me.

public class FacebookLogin extends Activity {

Facebook mFacebook = new Facebook("XXXXXXXXXXXX");
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
View linearLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    linearLayout = findViewById(R.id.main_layout);

    mFacebook.authorize(this, new DialogListener() {
        @Override
        public void onComplete(Bundle values) 
        {
            Log.d("Facebook-Example-Friends Request", "Started API request");
            mAsyncRunner.request("me/friends", new FriendsRequestListener());
            Log.d("Facebook-Example-Friends Request", "Finished API request");
        }

        @Override
        public void onFacebookError(FacebookError error) {}

        @Override
        public void onError(DialogError e) {}

        @Override
        public void onCancel() {}
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    mFacebook.authorizeCallback(requestCode, resultCode, data);
}

public class FriendsRequestListener implements RequestListener 
{
    /**
     * Called when the request to get friends has been completed.
     * Retrieve, parse and display the JSON stream.
     */
    public void onComplete(final String response)
    {
        Log.d("Facebook-Example-Friends Request", "FriendsListenerOnComplete");

        try 
        {
            JSONObject json = Util.parseJson(response);
            final JSONArray friends = json.getJSONArray("data");

            FacebookLogin.this.runOnUiThread(new Runnable()
            {
                public void run()
                {
                    int l = (friends != null ? friends.length() : 0);

                    for (int i=0; i<l; i++) 
                    {
                        try
                        {
                            JSONObject o = friends.getJSONObject(i);

                            TextView tv = new TextView(FacebookLogin.this);
                            tv.setText(o.getString("name"));
                            ((LinearLayout) linearLayout).addView(tv);
                        }
                        catch(JSONException e)
                        {
                            Toast.makeText(getApplicationContext(), "JSON parsing error", Toast.LENGTH_LONG);
                        }
                    }
                }
            }); 
        }
        catch(JSONException e)
        {
            Log.d("Facebook-Example-Friends Request", "JSON Error in response");
        }
        catch(FacebookError e)
        {
            Log.d("Facebook-Example-Friends Request", "Facebook Error in response");
        }   
    }
}

}

NewToAndroid
  • 581
  • 7
  • 25
  • Does the logcat write "Started API request"? If not try adding Log.d in onFacebookError, onError, onCancel to see what it says. – Daverix Oct 18 '12 at 17:24

1 Answers1

0

i think you see this answer you got some idea.. Facebook/Twitter Integration in my android application

and

http://android-sample-solution.blogspot.com

Community
  • 1
  • 1
Piyush
  • 3,061
  • 8
  • 34
  • 52