簡體   English   中英

無法從 GraphRequest Facebook 獲取值

[英]Cannot get values from GraphRequest Facebook

我正在使用Facebook SDK 4.16.1通過使用Profile我可以獲得first namelast namenameid我的代碼是這樣的

public class LoginActivity extends Activity {

String id, fname, lname, email, name, gender, locale, verified;
private CallbackManager callbackManager;
private AccessTokenTracker accessTokenTracker;

 @Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.loginactivity);

FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();

    accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
        }
    };

    LoginButton loginButton = (LoginButton)findViewById(R.id.login_button);
    loginButton.setReadPermissions(Arrays.asList(Constants.FACEBOOK_PERMISSIONS));

    FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile();

            FB_TOKEN=loginResult.getAccessToken().getToken();

            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {
                            // Application code
                            response.getError();
                            Log.e("JSON:", object.toString());

                            try {
                                email = object.getString("email");
                                gender = object.getString("gender");
                                locale = object.optString("locale");
                                verified = object.optString("verified");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,link,birthday,first_name,last_name,email,gender,verified,locale");
            request.setParameters(parameters);
            request.executeAsync();


            id = profile.getId();
            fname = profile.getFirstName();
            lname = profile.getLastName();
            name = profile.getName();

            nextActivity(profile);
            Toast.makeText(getApplicationContext(), "Logging in...", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel() {
        }

        @Override
        public void onError(FacebookException e) {
        }
    };

    loginButton.registerCallback(callbackManager, callback);
}

 }

我的常量看起來像這樣

public static final String[] FACEBOOK_PERMISSIONS = new String[] {
                                                                "public_profile",
                                                                "user_friends",
                                                                "email" };

我正在嘗試獲取電子郵件、性別、語言環境以及是否已經過驗證。 我將如何獲得其他信息? 對於Profile獲得的項目,我的總是空的

更新

我像這樣更新我的GraphRequest

GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {
                            // Application code
                            response.getError();
                            Log.e("JSON-RESULT:", object.toString());

                            JSONObject jsonObject = null;
                            try {
                                jsonObject = new JSONObject(object.toString());
                                email = jsonObject.getString("email");
                                gender = jsonObject.getString("gender");
                                locale = jsonObject.getString("locale");
                                verified = jsonObject.getString("verified");

                                Log.e(TAG, email + " This is the email");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,link,birthday,first_name,last_name,email,gender,verified,locale");
            request.setParameters(parameters);
            request.executeAsync();

在我的Logcat它顯示我正確設置了電子郵件和其他電子郵件,但是當您放置breakpoints時它不會進入GraphRequest ,因此我的全局variable電子郵件仍然為空。 即使在Log.e("JSON:", object.toString()); Log.e(TAG, email + " This is the email"); 它顯示我獲取並設置了值。 我想知道如何獲得Bundle的值?

嘗試使用下面的代碼來獲取其他信息.....

 private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
               // Log.d("Login", "onSuccess");
                GraphRequest.newMeRequest(
                        loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject me, GraphResponse response) {
                                if (response.getError() != null) {
                                    // handle error
                                } else {
                                    email = me.optString("email");
                                    String id = me.optString("id");
                                    String email=me.optString("email");
                                    String name= me.optString("name");
                                    String gender= me.optString("gender");
                                    String verified= me.optBoolean("is_verified") + "";                                    
                                }
                            }
                        }).executeAsync();
            }


            @Override
            public void onCancel () {
              //  Log.d("Login", "onCancel");
            }

            @Override
            public void onError (FacebookException e){
              //  Log.d("Login", "onError " + e);
               }
       };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM