其他分享
首页 > 其他分享> > android-Facebook登录中的片段

android-Facebook登录中的片段

作者:互联网

我想在我的应用程序中使用Facebook登录.我刚刚注册了应用程序,并将SDK添加到了项目中.但是,我尝试按照文档中的说明进行操作,但是没有任何效果(我需要从配置文件中获取ID和电子邮件,然后发送至我的服务器).

分段

public class LoginFragment extends Fragment {

    @BindView(R.id.ivLogo) ImageView ivLogo;
    @BindView(R.id.email) EditText edEmail;
    @BindView(R.id.password) EditText edPassword;
    @BindView(R.id.btnFbLogin) LoginButton btnFbLogin;

    private CallbackManager callbackManager;
    private AccessTokenTracker accessTokenTracker;
    private ProfileTracker profileTracker;

    String email, password, ID;
    User user;

    public LoginFragment() {
        // Required empty public constructor
    }

    public static LoginFragment newInstance() {
        LoginFragment fragment = new LoginFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        // App code
                        Log.i("success", "success");
                    }

                    @Override
                    public void onCancel() {
                        // App code
                    }

                    @Override
                    public void one rror(FacebookException exception) {
                        // App code
                    }
                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login_screen, container, false);
        ButterKnife.bind(this, view);

        btnFbLogin.setReadPermissions("email");
        btnFbLogin.setFragment(this);

        // Callback registration
        btnFbLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
            }

            @Override
            public void onCancel() {
                // App code
            }

            @Override
            public void one rror(FacebookException exception) {
                // App code
            }
        });

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        ((MainActivity) getActivity()).getSupportActionBar().hide();
    }

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

                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {

                                try {
                                    String email = object.getString("email");

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

            @Override
            public void onCancel() { }

            @Override
            public void one rror(FacebookException e) { }
        };

        btnFbLogin.setReadPermissions("email");
        btnFbLogin.registerCallback(callbackManager, callback);
    }


    private void loginEmail() {
        FactoryAPI.getInstanceLogin().login("hardcoded mail", "hardcoded password").enqueue(new Callback<UserResponse>() {
            @Override
            public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
                if (response.isSuccessful()) {
                    user = response.body().getUser();
                    startActivity();
                } else {
                    Toast.makeText(getContext(), R.string.email_password_is_not_right, Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<UserResponse> call, Throwable t) {
            }
        });
    }

    private void loginFB() {
        FactoryAPI.getServieFBlogin().loginFB(email, ID).enqueue(new Callback<UserResponse>() {
            @Override
            public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
                if (response.isSuccessful()) {
                    user = response.body().getUser();
                    startActivity();
                } else {
                    Toast.makeText(getContext(), "Something went wrong", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<UserResponse> call, Throwable t) {
                Log.e("error", "error");
            }
        });
    }

    public void getEmailPassword() {
        email = edEmail.getText().toString();
        password = edPassword.getText().toString();
        if (email.isEmpty() || password.isEmpty()) {
            Toast.makeText(getContext(), R.string.empty_properties, Toast.LENGTH_SHORT).show();
        }
    }

    public static boolean emailValidation(CharSequence target) {
        return !TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }

    public void startActivity() {
        Intent intent = new Intent(getContext(), AccountActivity.class);
        intent.putExtra("account", user);
        startActivity(intent);
    }

    @Override
    public void onActivityResult(int requestCode, int responseCode, Intent intent) {
        super.onActivityResult(requestCode, responseCode, intent);
        callbackManager.onActivityResult(requestCode, responseCode, intent);
    }

    @OnClick({R.id.sign_up_email, R.id.btnFbLogin})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sign_up_email:
                loginEmail();
            case R.id.btnFbLogin:
                break;
        }
    }

    private void getProfile(Profile profile){
        if(profile != null){
            ID = profile.getId();
        }
    }
}

解决方法:

由于您的Facebook登录名处于Fragment不在活动中,因此
回调来自此片段所附的Activity的onActivityResult().

您可以在覆盖活动的onActivityResult()之后进行检查,然后在其中放置调试点.

获得活动onActivityResult()方法的结果后,可以将其发送到片段的onActivityResult().

希望这会有所帮助.

标签:android-fragments,facebook,facebook-login,android,facebook-sdk-4-0
来源: https://codeday.me/bug/20191026/1938771.html