我正在使用以下代码,使用用于Android的facebook sdk从用户ID中检索用户的姓名和性别。
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,而不返回性别。如何获得用户的性别?
最佳答案
您使用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();
}
关于android - Graph Api在Android上未返回性别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24058413/