gpt4 book ai didi

android - FIrebase Java/Android createUser 失败

转载 作者:可可西里 更新时间:2023-10-31 22:04:12 25 4
gpt4 key购买 nike

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

下面的打印语句的输出是:

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

Process finished with exit code 0

尽管我只是在 Mac OS 上将我的测试作为 Java 应用程序运行,但代码使用 firebase-client-android-2.2.3.jar 文件作为 Firebase 类。稍后这将进入 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();
}

}

}

最佳答案

您需要在程序末尾添加一个Thread.sleep。很可能您的程序在 Firebase 有机会向服务器发送任何内容之前退出。

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

关于android - FIrebase Java/Android createUser 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29975024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com