简体   繁体   中英

android get facebook username and id

i have very strange situetion. I 'm working facebook sdk in android. i wrote login code and also can check username and user id.but this code does not working when divice hase facebook application(i uninstalled it and then worked perfect) this is a my code

public void LoginFacebook() {

    mFacebook.authorize(this, mPermissions, new LoginDialogListener());

}

private final class LoginDialogListener implements DialogListener {

    public void onComplete(Bundle values) {
        SessionStore.save(mFacebook, getApplicationContext());

        getProfileInformation();

    }

    public void onCancel() {
        SessionEvents.onLoginError("Action Canceled");
    }

    @Override
    public void onFacebookError(FacebookError error) {
        SessionEvents.onLoginError(error.getMessage());

    }

    @Override
    public void onError(DialogError error) {
        SessionEvents.onLoginError(error.getMessage());

    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


}

@SuppressLint("SdCardPath")
public void getProfileInformation() {

    mAsyncRunner.request("me", new BaseRequestListener() {

        @SuppressLint("CommitPrefEdits")
        @Override
        public void onComplete(String response, Object state) {

            final String json = response;

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    try {

                        JSONObject profile = new JSONObject(json);
                        facebook_userid = profile.getString("id");
                        facebook_username = profile.getString("name");
                        facebook_username = facebook_username.replace(
                                "%20", " ");

                        editor.putString("fb_name", facebook_username);

                        editor.putString("fb_id", facebook_userid);

                        fb_name.setText(facebook_username);

                        getFacebookAvatar(facebook_userid);
                        editor.commit();
                    } 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) {
        }
    });
}

i have no idea what is a wrong. if anyone knows solution please help me thanks

启用客户端深层链接,“单点登录”以及“ devlopers.facebook.com”上的“ OAuth登录”选项卡

You Can Do Like This.

public static void getuserdata() throws JSONException {
    String Facebook = readT("https://graph.facebook.com/me?access_token="+mFacebook.getAccessToken()+"&fields=id,username)".replace(" ","%20"));
    try
    {
        JSONObject jsonobj = new JSONObject(Facebook);
        fb_id = jsonobj.getString("id");
        fb_username = fb_fname+" "+fb_lname;
        Log.e("..fb_username..........", fb_username);
    }
    catch (JSONException e) 
    {
        e.printStackTrace();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM