无法写入数据库Firebase

时间:2018-04-26 16:41:53

标签: android firebase

mDatabse = FirebaseDatabase.getInstance().getReference().child("Users");

HashMap<String, String> usermMap = new HashMap<>();
usermMap.put("name", display_name);
usermMap.put("status", "Hey, ich benutze die App.");
usermMap.put("image", "default_pic");
usermMap.put("thumb_image", "default_pic");

mDatabse.setValue(usermMap);

仅注册此帐户但不创建数据库数据。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "de.epix_crew.epixchat"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    //implementation 'com.android.support:design-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'com.google.firebase:firebase-auth:15.0.0'
    implementation 'com.android.support:support-v4:27.1.1'
    implementation 'com.google.firebase:firebase-database:15.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:design:27.1.1'
    implementation 'de.hdodenhof:circleimageview:2.2.0'


}

apply plugin: 'com.google.gms.google-services'

这是这个文件,希望你能帮我把它搞定。 也许如果你有Teamviewer,你可以看到问题:)

也谢谢:)

2 个答案:

答案 0 :(得分:0)

好的,我从您的代码中提取了firebase所需的最少必要的东西并尝试了它。它适用于我的测试数据库。 这是我使用的代码:

public class MainActivity extends AppCompatActivity {

    private DatabaseReference mDatabse;
    private FirebaseAuth mAuth;
    private Button mCreateBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAuth = FirebaseAuth.getInstance();

        mCreateBtn = (Button) findViewById(R.id.btn);
        mCreateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String display_name = "Otto";
                String email = "mmuuuu@email.com";
                String password = "111111";

                if(!TextUtils.isEmpty(display_name) && !TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)){
                    register_user(display_name, email, password);
                }
            }
        });
    }

    private void register_user(final String display_name, String email, String password) {
        mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {

                    FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();
                    String uid = current_user.getUid();

                    mDatabse = FirebaseDatabase.getInstance().getReference().child("Users");

                    HashMap<String, String> usermMap = new HashMap<>();
                    usermMap.put("name", display_name);
                    usermMap.put("status", "Hey, ich benutze die - App.");
                    usermMap.put("image", "default_pic");
                    usermMap.put("thumb_image", "default_pic");

                    mDatabse.setValue(usermMap);

                } else {
                    Toast.makeText(MainActivity.this, "Fehler! Überbrüfe noch einmal die Eingabefelder!", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

(注意,我没有使用EditText字段,但为了简化,将值直接放在String变量中。)

以下是我的数据库规则:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

这是我数据库中的书面数据。 (我将它导出到JSON文件中):

"Users" : {
  "image" : "default_pic",
  "name" : "Otto",
  "status" : "Hey, ich benutze die - App.",
  "thumb_image" : "default_pic"
},

答案 1 :(得分:0)

if (task.isSuccessful()) {

                FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();
                String uid = current_user.getUid();

                Log.d("Inside if task successful",".......");
                // added a log to check if excecution gets inside the if block
                mDatabse = FirebaseDatabase.getInstance().getReference().child("Users").child(uid);

                HashMap<String, String> usermMap = new HashMap<>();
                usermMap.put("name", display_name);
                usermMap.put("status", "Hey, ich benutze die - App.");
                usermMap.put("image", "default_pic");
                usermMap.put("thumb_image", "default_pic");

                mDatabse.setValue(usermMap);



                /*
                mRegProgress.dismiss();

                Intent mainIntent = new Intent(RegisterActivity.this, MainActivity.class);
                mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(mainIntent);
                finish();

                */

            } else {
                mRegProgress.hide();
                Toast.makeText(RegisterActivity.this, "Fehler! Überbrüfe noch einmal die Eingabefelder!", Toast.LENGTH_LONG).show();
            }

在if块中添加了一个日志。