与Facebook的连接正常,但是回到MainActivity时我丢失了数据

时间:2019-03-07 04:47:14

标签: java android facebook-graph-api

我将开发一个android应用程序,并尝试分离事物。...我创建了一个FacebookConnect类来调用facebook API,以检索facebook公共信息,以便以后使用它们,但当我的对象为null时我回到了我的主要Java课。

任何想法...我的知识是该技能领域的平均水平。

这是我在FacebookConnect中的功能

public void getConnectionWithFacebook(Activity myActivity, CallbackManager mCallbackManager){
    MCALLBACKMANAGER = mCallbackManager;
    MYACTIVITY = myActivity;
    ONEUSER = new User();

    LoginManager.getInstance().logInWithReadPermissions(MYACTIVITY, Arrays.asList("email", "public_profile"));
    LoginManager.getInstance().registerCallback(MCALLBACKMANAGER, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.d(TAG,"facebookSuccess3 -> " + loginResult.toString());
            // App code
            GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {
                    Log.d(TAG,"facebookSuccess4 -> " + response.toString());
                    getDataFromFacebook(object);
                }
            });
            Bundle parameters = new Bundle();
            parameters.putString("fields","id,email,first_name,last_name");
            request.setParameters(parameters);
            request.executeAsync();
        }

        @Override
        public void onCancel() {
            Log.d(TAG,"facebook -> " + "onCancel");
            Toast.makeText(MYACTIVITY, "Une erreur s'est produite, veuillez réessayer plus tard...", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(FacebookException error) {
            Log.d(TAG,"facebookONError -> " +error.toString());
            Toast.makeText(MYACTIVITY, "Une erreur s'est produite, veuillez réessayer plus tard...", Toast.LENGTH_LONG).show();
            // ...
        }
    });
}

在我的主类中,我将在click事件中调用此函数:

btnFBLogin.setOnClickListener(new View.OnClickListener() {
        FacebookConnect oneConnection = new FacebookConnect();
        @Override
        public void onClick(View v) {
            btnFBLogin.setEnabled(false);
            oneConnection.getConnectionWithFacebook(MainActivity.this, mCallbackManager);
            String email = oneConnection.getEmail();
            Log.d(TAG,"handleFacebookToken1 -> " + email); // doesnt work
        }
    });

不幸的是,结果为null或为空,我不知道为什么。

编辑:

这是我的功能,用于将来自Facebook的数据关联到我的班级:

public void getDataFromFacebook(JSONObject object) {
    try{
        String emailJSON = object.getString("email");
        String fristNameJSON = object.getString("first_name");
        String lastNameJSON = object.getString("last_name");

        Log.d(TAGUSER,"facebookSuccess -> " + emailJSON);
        Log.d(TAGUSER,"facebookSuccess -> " + fristNameJSON);
        Log.d(TAGUSER,"facebookSuccess -> " + lastNameJSON);

        //Log.d(TAGUSER,"facebookSuccess -> " + object.getString("email"));
        // Log.d(TAGUSER,"facebookSuccess -> " + object.getString("first_name"));
        //Log.d(TAGUSER,"facebookSuccess -> " + object.getString("last_name"));
        this.setEmail(emailJSON);
        this.setFirstName(fristNameJSON);
        this.setLastName(lastNameJSON);

        Log.d(TAGUSER,"facebookSuccess -> " + getEmail());
        Log.d(TAGUSER,"facebookSuccess -> " + getFirstName());
        Log.d(TAGUSER,"facebookSuccess -> " + getLastName());

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

我在每个地方都投入了很多东西来查看数据删除的情况

再次感谢!

0 个答案:

没有答案