如何在Android中获取FB朋友生日

时间:2013-01-09 08:59:25

标签: android facebook facebook-graph-api

我正在使用此示例代码https://github.com/chrtatu/FacebookFriendsList来获取FB朋友的名单和个人资料照片。
在这里我也尝试获取他们的DOB,但是代替DOB,我得到了 null 值。
请帮助获取。

我的代码:

  String[] facebook_permissions = { "user_photos", "friends_birthday", "friends_photos" };

  ==========================================================================

   public class FriendListAdapter extends BaseAdapter implements SectionIndexer {
    private LayoutInflater mInflater;
    private String[] sections;
    private GetProfilePictures picturesGatherer = null;
    FriendsList friendsList;
    Hashtable<Integer, FriendItem> listofshit = null;

    public FriendListAdapter(FriendsList friendsList) {
        Log.d(LOG_TAG, "FriendListAdapter()");
        this.friendsList = friendsList;
        sections = new String[getCount()];
        listofshit = new Hashtable<Integer, FriendItem>();
        for (int i = 0; i < getCount(); i++) {
            try {
                sections[i] = jsonArray.getJSONObject(i).getString("name").substring(0, 1);
                sections[i] = jsonArray.getJSONObject(i).getString("birthday").substring(0, 2);
            } catch (JSONException e) {
                sections[i] = "";
                Log.e(LOG_TAG, "getJSONObject: " + e.getMessage());
            }
        }
        if (picturesGatherer == null) {
            picturesGatherer = new GetProfilePictures();
        }
        picturesGatherer.setAdapterForListener(this);
        mInflater = LayoutInflater.from(friendsList.getBaseContext());
    }

    public int getCount() {
        Log.d(LOG_TAG, "getCount()");
        if (jsonArray == null)
            return 0;
        return jsonArray.length();
    }

    public Object getItem(int position) {
        Log.d(LOG_TAG, "getItem()");
        return listofshit.get(position);
    }

    public long getItemId(int position) {
        Log.d(LOG_TAG, "getItemId()");
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d(LOG_TAG, "getView(" + position + ")");

        JSONObject jsonObject = null;
        try {
            jsonObject = jsonArray.getJSONObject(position);
        } catch (JSONException e) {
            Log.e(LOG_TAG, "getJSONObject: " + e.getMessage());
        }

        FriendItem friendItem;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.single_friend, null);
            friendItem = new FriendItem();

            convertView.setTag(friendItem);
        }
        else {
            friendItem = (FriendItem) convertView.getTag();
        }

        friendItem.friendPicture = (ImageView) convertView.findViewById(R.id.picture_square);
        friendItem.friendName = (TextView) convertView.findViewById(R.id.name);
        friendItem.friendDob = (TextView) convertView.findViewById(R.id.dob);
        friendItem.friendLayout = (RelativeLayout) convertView.findViewById(R.id.friend_item);

        try {
            String uid = jsonObject.getString("uid");
            String url = jsonObject.getString("pic_square");
            friendItem.friendPicture.setImageBitmap(picturesGatherer.getPicture(uid, url));
        } catch (JSONException e) {
            Log.e(LOG_TAG, "getJSONObject: " + e.getMessage());
            friendItem.friendName.setText("");
            friendItem.friendDob.setText("");
        }

        try {
            friendItem.friendName.setText(jsonObject.getString("name"));
            friendItem.friendDob.setText(jsonObject.getString("birthday"));
        } catch (JSONException e) {
            Log.e(LOG_TAG, "getJSONObject: " + e.getMessage());
            friendItem.friendName.setText("");
            friendItem.friendDob.setText("");
        }

        listofshit.put(position, friendItem);

        return convertView;
    }

    public int getPositionForSection(int position) {
        return position;
    }

    public int getSectionForPosition(int position) {
        return position;
    }

    public Object[] getSections() {
        return sections;
    }
}

class FriendItem {
    TextView friendDob;
    int id;
    ImageView friendPicture;
    TextView friendName;
    RelativeLayout friendLayout;

}

==============================================================================

String query = "select name, uid, pic_square, birthday from user where uid in 
(select uid2 from friend where uid1=me()) order by name";

2 个答案:

答案 0 :(得分:3)

而不是这两行代码:

 sections[i] = jsonArray.getJSONObject(i).getString("name").substring(0, 1);
 sections[i] = jsonArray.getJSONObject(i).getString("birthday").substring(0, 2);

使用以下两行:

sections[i] = jsonArray.getJSONObject(i).getString("name").substring(0);
sections[i] = jsonArray.getJSONObject(i).getString("birthday").substring(1);

答案 1 :(得分:1)

请使用以下方法:并且也看一下这个例子

http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/

public void getProfileInformation() {
mAsyncRunner.request("me", new RequestListener() {
    @Override
    public void onComplete(String response, Object state) {
        Log.d("Profile", response);
        String json = response;
        try {
            JSONObject profile = new JSONObject(json);
            // getting name of the user
            String name = profile.getString("name");
            // getting email of the user
            String email = profile.getString("email");
            //getting user birthday
            String birth_day=profile.getString("birthday");

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
                }

            });

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onIOException(IOException e, Object state) {
    }

    @Override
    public void onFileNotFoundException(FileNotFoundException e,
            Object state) {
    }

    @Override
    public void onMalformedURLException(MalformedURLException e,
            Object state) {
    }

    @Override
    public void onFacebookError(FacebookError e, Object state) {
    }
});