java create user,火力的Java / Android的的createUser失败

I'm trying to write a simple test of the Firebase user creation and authentication routines so that I can test my Firebase security settings. The code runs smoothly but the callbacks are not invoked and no users are created on the Firebase side.

The output of below with the print statements is:

Begin process

Start Create User

Creating User

End Creating User

End process

Process finished with exit code 0

The code is using the firebase-client-android-2.2.3.jar file for the Firebase classes though I'm just running my test as a java application on a Mac OS. Later this will go into an Android app but I'd like to be able to run it inside my IDE for now. Any insights from experienced Firebase coders much appreciated.

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://.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>() {

@Override

public void onSuccess(Map 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 authMap = new HashMap();

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();

}

}

}

解决方案

You'll want to add a Thread.sleep at the end of the program. Likely your program exits before Firebase gets a chance to send anything to the server.

A more proper solution would be to introduce actual lifecycle management into your app, e.g. waiting for the createUser call to finish. But given that you'll be migrating this to Android (which handles app lifecycle completely different anyway) that might not be worth the effort.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值