0

I am new in Facebook integration with android. I've logged in to Facebook in my android application.

So I am creating an app which needs to get the Facebook friends list (the names and the pictures). I Googled but not getting. How can I get the list of friends?

XML file, when button is clicked i want my all list of Facebook friends

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"  
android:orientation="vertical">

<ImageView 
    android:id="@+id/login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/login_button"/>

<ImageView 
    android:id="@+id/picturepic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"/>

  <Button
      android:id="@+id/btn_get_friend_list"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Friends"
      android:layout_marginTop="30dip"
      android:layout_gravity="center_horizontal"
      android:visibility="gone"/>

 <Button
      android:id="@+id/btn_show_access_tokens"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Show Access Tokens"
      android:layout_marginTop="30dip"
      android:layout_gravity="center_horizontal"
      android:visibility="gone"/>

package com.example.simplelogin1;

 import java.io.IOException;
   import java.net.MalformedURLException;
 import com.facebook.android.AsyncFacebookRunner;
   import com.facebook.android.DialogError;
  import com.facebook.android.Facebook;
   import com.facebook.android.FacebookError;
  import com.facebook.android.Facebook.DialogListener;
 import android.os.Bundle;
 import android.app.Activity;
import android.content.Intent;
 import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
 import android.view.View.OnClickListener;
import android.widget.Button;
 import android.widget.ImageView;
  import android.widget.Toast;

   public class MainActivity extends Activity implements OnClickListener 
   {
ImageView pic,button;
Facebook fb;
Button btnShowAccessTokens,btnGetFriendList;

private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;   

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String APP_ID="";
    fb=new Facebook(APP_ID);

    btnGetFriendList=(Button) findViewById(R.id.btn_get_friend_list);
    btnShowAccessTokens = (Button) findViewById(R.id.btn_show_access_tokens);       
    button=(ImageView) findViewById(R.id.login);
    pic=(ImageView) findViewById(R.id.picturepic);
    mAsyncRunner = new AsyncFacebookRunner(fb);
    button.setOnClickListener(this);

    btnGetFriendList.setOnClickListener(this);


    btnShowAccessTokens.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            showAccessTokens();
        }
    });     

    updateButtonImage();
}

private void updateButtonImage() 
{
    // TODO Auto-generated method stub
    if(fb.isSessionValid())
    {
        button.setImageResource(R.drawable.logout_button);
    }
    else
    {
        button.setImageResource(R.drawable.login_button);
    }
}

@Override
public void onClick(View v) 
{
    // TODO Auto-generated method stub
    if(fb.isSessionValid())
    {
        try
        {
            fb.logout(getApplicationContext());
            updateButtonImage();
            btnShowAccessTokens.setVisibility(View.INVISIBLE);
            btnGetFriendList.setVisibility(View.INVISIBLE);
        } 
        catch (MalformedURLException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    else
    {
        mPrefs = getPreferences(MODE_PRIVATE);
        String access_token = mPrefs.getString("access_token", null);
        long expires = mPrefs.getLong("access_expires", 0);
        if (access_token != null) 
        {
            fb.setAccessToken(access_token);                    
            // Making show access tokens button visible
            btnShowAccessTokens.setVisibility(View.VISIBLE);
            btnGetFriendList.setVisibility(View.VISIBLE);
            Log.d("FB Sessions", "" + fb.isSessionValid());
        }
        if (expires != 0) 
        {
            fb.setAccessExpires(expires);
        }

        {
        fb.authorize(this,new String[] {"email","publish_stream"} , new DialogListener() 
        {

            @Override
            public void onFacebookError(FacebookError e)
            {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "fbError", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(DialogError e) 
            {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onComplete(Bundle values)
            {
                // TODO Auto-generated method stub      
                updateButtonImage();
                // Function to handle complete event
                // Edit Preferences and update facebook acess_token
                SharedPreferences.Editor editor = mPrefs.edit();
                editor.putString("access_token",fb.getAccessToken());
                editor.putLong("access_expires",fb.getAccessExpires());
                editor.commit();                    
                btnShowAccessTokens.setVisibility(View.VISIBLE);    
                btnGetFriendList.setVisibility(View.VISIBLE);
            }               
            @Override
            public void onCancel()
            {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "onCancel", Toast.LENGTH_SHORT).show();
            }
        });         
    }
}
  }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    fb.authorizeCallback(requestCode, resultCode, data);
}

public void showAccessTokens() 
{
    String access_token = fb.getAccessToken();
    Toast.makeText(getApplicationContext(),"Access Token: " + access_token, Toast.LENGTH_LONG).show();
}
 }

My main activity ,,where should i implement getting a friends from Facebook when i clicked button.

Hemanth11MS
  • 23
  • 1
  • 7
  • possible duplicate of [Get a list of Friends of a Friend on facebook](http://stackoverflow.com/questions/1330123/get-a-list-of-friends-of-a-friend-on-facebook) – Chintan Khetiya Dec 25 '13 at 07:30
  • @chintankhetiya please tell me how to implement in my code.. – Hemanth11MS Dec 25 '13 at 07:34
  • Here is an Facebook Application with retrieving List of Facebook Friends. Once go through this [link](https://www.dropbox.com/s/0pfygkrzhv19wfv/FriendPickerSample.rar) – Mr. N.V.Rao Dec 25 '13 at 08:50

1 Answers1

0

after success full login

AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
String query="SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())";
            Bundle bundal=new Bundle();
            bundal.putString("method", "fql.query");
            bundal.putString("query", query);
            return bundal;
    mAsyncRunner.request(bundal, new RequestListener() {
                    @Override
                    public void onComplete(final String response, Object state) {
                        // TODO Auto-generated method stub
                        //parse json and get all friends        
                    }
                });
Shahjahan Khan
  • 189
  • 1
  • 8