我使用以下代码从用户ID使用facebook sdk for android检索用户名和性别。
protected void getUserDetails(ArrayList<String> ids) {
// TODO Auto-generated method stub
Bundle params=new Bundle();
params.putString("fields","name,gender");
RequestBatch rb=new RequestBatch();
Request request=null;
users=new ArrayList<UserContainer>();
for(String userid:ids)
{
request=newRequest(Session.getActiveSession(),"/"+userid,params,HttpMethod.GET,userCallBack);
rb.add(request);
}
rb.executeAsync();
}
Request.Callback userCallBack=new Request.Callback() {
@Override
public void onCompleted(Response response) {
// TODO Auto-generated method stub
System.out.println(response.toString());
JSONObject object=response.getGraphObject().getInnerJSONObject();
UserContainer user=new UserContainer();
try {
if(object.has("name"))
{
user.setName(object.getString("name"));
}
if(object.has("gender"))
{
System.out.println("Gender:"+object.getString("gender"));
user.setGender(object.getString("Gender"));
}
else
user.setGender("N/A");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
users.add(user);
showLikers(users);
}
};
上面的代码只返回名称和ID,而不是性别。如何获得用户的性别?
答案 0 :(得分:1)
您使用3.x api这很有用,要为此值分隔权限,您可以获得此类性别并查看Facebook gender
private String UserInfoDisplay(GraphUser user) {
StringBuilder userInfo = new StringBuilder("");
String Name = user.getName();
String Id = user.getId();
String lastname = user.getLastName();
String firstname = user.getFirstName();
String getUsername = user.getUsername();
String get_gender = (String) user.getProperty("gender");
String image_url = "http://graph.facebook.com/"+user.getId()+"/picture?type=square";
Log.i("User information == >", Name +Id + lastname+
firstname + image_url + get_gender + getUsername + "usermail"+User_mail );
text.setText(Name + " mail = " +User_mail);
return userInfo.toString();
}