获取用户位置
问题描述:
这就是我想要做的目前获取用户位置
permissions.add("user_friends");
permissions.add("email");
permissions.add("public_profile");
permissions.add("user_location");
在我捆绑
而且我传递参数
Bundle bundle = new Bundle();
bundle.putString("fields", "id,name,picture,cover,email,location,accounts");
不幸的是,响应不包含位置对象。
偶试过这种方法,但崩溃,并将响应为空或一些错误
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me?fields=location",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
).executeAsync();
答
许多个月后,我知道,但任何人都可能需要在这里是为我工作:
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
URL profile_pic=null;
try
{
String id = object.getString("id"); // get_id
try {
profile_pic = new URL("https://graph.facebook.com/" + id + "/picture?width=150&height=150");
} catch (MalformedURLException e) {
e.printStackTrace();
}
String pic = profile_pic.toString(); //get profile pic to string to decode later to bitmap if needed
String name = object.getString("name"); // get username
String email = object.getString("email"); //get user email
String location = object.getJSONObject("location").getString("name"); //get location
System.out.println(id + ", " + name + ", " + email + " " + location);
}
catch (JSONException e)
{
e.printStackTrace();
}
Intent intent = new Intent(StartingActivity.this, SignUpFinalise.class);
startActivity(intent);
}
这里的逻辑是响应对象中的位置是对象本身。因此需要调用getJSONObject,以便我们可以深入其树,并获取名称字段,这是实际位置 您可以在此处看到响应对象的示例: https://developers.facebook.com/docs/android/graph