从Graph API获取用户位置

时间:2016-03-07 11:18:40

标签: android

这就是我目前正在尝试做的事情

    permissions.add("user_friends");
    permissions.add("email");
    permissions.add("public_profile");
    permissions.add("user_location");

同样在我的Bundle中我传递了参数

    Bundle bundle = new Bundle();
    bundle.putString("fields", "id,name,picture,cover,email,location,accounts");

不幸的是,Response不包含位置对象。

即使尝试了这种方法,但由于响应为空或出现错误而崩溃

new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me?fields=location",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
    public void onCompleted(GraphResponse response) {
        /* handle the result */
    }
}
).executeAsync();

1 个答案:

答案 0 :(得分:0)

许多个月后,我知道,但对于任何可能需要它的人来说,这对我有用:

 @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,以便我们可以在其树中更深入并获取“name”字段,这是实际位置 您可以在此处查看响应对象的示例: https://developers.facebook.com/docs/android/graph