-1

如何在下面的 getProfileInformation() 中获取 facebook 用户 ID 以及电子邮件、名称,这里我想获取用户 ID,并希望在 run() 中与电子邮件和名称一起显示在 toast 消息中;

public void getProfileInformation() {
    mAsyncRunner.request("me", new RequestListener() {
        @Override
        public void onComplete(String response, Object state) {
            Log.d("Profile", response);
            String json = response;
            try {
                JSONObject profile = new JSONObject(json);
                // getting name of the user
                final String name = profile.getString("name");
                // getting email of the user
                final String email = profile.getString("email"); 
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() { 
                        Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email , Toast.LENGTH_LONG).show();
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onIOException(IOException e, Object state) {
        }

        @Override
        public void onFileNotFoundException(FileNotFoundException e,
                Object state) {
        }

        @Override
        public void onMalformedURLException(MalformedURLException e,
                Object state) {
        }

        @Override
        public void onFacebookError(FacebookError e, Object state) {
        }
    });
}
4

1 回答 1

3

您可以在请求中传递 Bundle 中的必填字段:

public void getProfileInformation() {
    Bundle params = new Bundle();
    params.putString("fields", "id,name,email");

    mAsyncRunner.request("me", params, new RequestListener() {
    @Override
    public void onComplete(String response, Object state) {
        Log.d("Profile", response);
        String json = response;
        try {
            JSONObject profile = new JSONObject(json);
            // getting name of the user
            final String name = profile.getString("name");
            // getting email of the user
            final String email = profile.getString("email"); 
            final Long id = profile.getLong("id");
            runOnUiThread(new Runnable() {
                @Override
                public void run() { 
                    Toast.makeText(getApplicationContext(), "ID:" + id + "\nName: " + name + "\nEmail: " + email , Toast.LENGTH_LONG).show();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
...

请注意,您将需要“电子邮件”权限。

于 2012-09-23T18:42:20.023 回答