2013-01-09 22 views
0

我使用此示例代码https://github.com/chrtatu/FacebookFriendsList来获取FB朋友名单和他们的名字和个人资料图片。
在这里,我尝试获得他们的DOB,但是代替DOB,我得到了null值。
请帮忙取回。如何在Android中获取FB朋友生日

我的代码:

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"; 
+0

所以你没有得到**的** DOB。这意味着你已经成功地提取了他们的名字和个人资料照片。 –

+0

它显示getJSONObject:没有生日日期的值...所以我们需要尝试一些其他关键字或许可字... – itsrajesh4uguys

+1

看看这里,http://stackoverflow.com/a/5350705/1626878 –

回答

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); 
+0

你得到了答案... :) – SilentKiller

+0

它可以帮助我。谢谢你分享代码。 +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) { 
    } 
}); 
+0

为什么我无法获取位置,生日? –

相关问题