FIrebase Java / Android createUser失败

时间:2015-04-30 18:23:29

标签: android firebase firebase-authentication firebase-realtime-database

我正在尝试编写Firebase用户创建和身份验证例程的简单测试,以便我可以测试Firebase安全设置。代码运行顺畅,但不调用回调,也没有在Firebase端创建用户。

下面带有print语句的输出是:

Begin process
Start Create User
Creating User
End Creating User
End process

Process finished with exit code 0

代码使用firebase-client-android-2.2.3.jar文件作为Firebase类,虽然我只是在Mac OS上将我的测试作为Java应用程序运行。稍后这将进入Android应用程序,但我现在想在我的IDE中运行它。来自经验丰富的Firebase程序员的任何见解都非常赞赏。

import com.firebase.client.Firebase;
import com.firebase.client.AuthData;
import com.firebase.client.FirebaseError;

import java.util.*;

public class FireRulesTest {


static String firebase_baseUrl = "https://<myfirebase>.firebaseio.com/";

public static void main(String[] args)
        throws FirebaseException {

    System.out.println("Begin process");

    FireRulesTest tester = new FireRulesTest();
    tester.createUser();

    System.out.println("End process");
}

private void createUser()
        throws FirebaseException {

    try {
        System.out.println("Start Create User");

        final String mEmail = "me@email.com";
        final String mPassword = "password";

        final Firebase ref = new Firebase(firebase_baseUrl);
        System.out.println("Creating User");

        ref.createUser(mEmail, mPassword,
                new Firebase.ValueResultHandler<Map<String, Object>>() {

                    @Override
                    public void onSuccess(Map<String, Object> result) {
                        System.out.println("Successfully created user account with uid: " + result.get("uid"));
                        ref.authWithPassword(mEmail, mPassword, new Firebase.AuthResultHandler() {
                            @Override
                            public void onAuthenticated(AuthData authData) {
                                //success, save auth data
                                HashMap<String, Object> authMap = new HashMap<String, Object>();
                                authMap.put("uid", authData.getUid());
                                authMap.put("token", authData.getToken());
                                authMap.put("email", mEmail);
                                authMap.put("password", mPassword);

                                Firebase currentUserRef = new Firebase(firebase_baseUrl + "movo/users/" + authData.getUid());
                                authMap.put("currentUser", currentUserRef);

                                System.out.println("User ID: " + authData.getUid() +
                                        ", Provider: " + authData.getProvider() +
                                        ", Expires:" + authData.getExpires());


                                System.out.println("Authentication complete");
                            }

                            @Override
                            public void onAuthenticationError(FirebaseError firebaseError) {
                                System.out.println("Authentication Error authenticating newly created user. This could be an issue. ");
                                System.out.println(firebaseError.getMessage());
                            }

                        });

                        }

                        @Override
                        public void onError(FirebaseError firebaseError) {
                        System.out.println("On Error authenticating newly created user. This could be an issue. ");
                            System.out.println(firebaseError.getMessage());
                        }
            });

            System.out.println("End Creating User");

        } catch (Exception fbe) {
            System.out.println("Exception: " + fbe.getMessage());
            fbe.printStackTrace();
        }

    }

}

1 个答案:

答案 0 :(得分:3)

您需要在程序结束时添加Thread.sleep。在Firebase有机会向服务器发送任何内容之前,您的程序可能会退出。

更合适的解决方案是将实际生命周期管理引入您的应用,例如:等待createUser来电完成。但鉴于您将把它迁移到Android(它完全不同地处理应用程序生命周期),这可能不值得付出努力。