从Facebook获取用户所在的国家

时间:2016-03-29 06:53:21

标签: android facebook facebook-graph-api

我正试图从Facebook获取用户当前的国家/地区。我能够获取电子邮件,身份证,姓氏,名字等数据,但是当涉及到国家时,我找不到合适的代码。有人可以告诉我所需的权限以及如何从对象中获取数据。

目前这些是我的权限

loginButton.setReadPermissions(Arrays.asList("public_profile", "email"));

我正在提取这样的数据

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {
                    Log.d("JSON", "" + response.getJSONObject().toString());
                    try {
                        name = object.getString("name");
                        first_name = object.optString("first_name");
                        last_name = object.optString("last_name");
                        fname.setText(first_name);
                        lname.setText(last_name);
                        e_mail.setText(email);
                        dp1.setProfileId(object.optString("id"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
            /*AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile();
            displayMessage(profile);*/
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,first_name,last_name,email,picture.type(large)");
            graphRequest.setParameters(parameters);
            graphRequest.executeAsync();
        }

1 个答案:

答案 0 :(得分:0)

要获取当前位置,请按以下步骤操作:

只需请求user_location之后的正确权限,然后

修改

使用以下代码行:

 String currentCity = obj.getJSONObject("location").getString("name");

为:

                  try {
                        name = object.getString("name");
                        first_name = object.optString("first_name");
                        last_name = object.optString("last_name");
                        String currentCity = obj.getJSONObject("location").getString("name");
                        fname.setText(first_name);
                        lname.setText(last_name);
                        e_mail.setText(email);
                        dp1.setProfileId(object.optString("id"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

还在以下内容中添加location

  parameters.putString("fields", "id,name,first_name,last_name,email,location,picture.type(large)");