-1

How can I get facebook user id along with email,name in the following getProfileInformation(), here I would like to get the user id, and want to show in a toast message along with email and name in the run();

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

リクエストのバンドルで必須フィールドを渡すことができます。

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 に答える