数据无法存储在实时数据库中

时间:2019-06-08 06:29:25

标签: android firebase firebase-realtime-database

我通过在孩子的电话号码(数字)中添加一个孩子作为“用户”和另一个孩子来输入用户名和电话号码。

我放置了一个addOnCompleteListener,它告诉我任务是否成功。

它总是说任务没有成功,因为我已经添加了Toast“网络错误”,当我尝试向数据库中添加数据时,它向我显示了网络错误

 private void AddAcc(final String nam, final String mnum)
  {
    final DatabaseReference Rootref;
    Rootref= FirebaseDatabase.getInstance().getReference();

    Rootref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            if (!(dataSnapshot.child("Users").child(mnum).exists())) {
                HashMap<String, Object> userDataMap = new HashMap<>();
                userDataMap.put("Name", nam);
                userDataMap.put("Phone No.", mnum);
                Rootref.child("Users").child(mnum).updateChildren(userDataMap)
                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {

                                if (task.isSuccessful()) {
                                    Toast.makeText(SignUp.this, "Data Stored", Toast.LENGTH_SHORT).show();
                                    loadingBar.dismiss();
                                } else {
                                    Toast.makeText(SignUp.this, "Network Error", Toast.LENGTH_SHORT).show();
                                    loadingBar.dismiss();
                                }
                            }
                        });


            } else {
                loadingBar.dismiss();
                Toast.makeText(SignUp.this, "Number is Already Present", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

无论我做什么,它总是弹出“网络错误”吐司

1 个答案:

答案 0 :(得分:0)

这是“电话号码”中的句点,Firebase认为该令牌无效。将来的参考可以调用task.getException();。它将为您提供失败的更多原因。编辑。希望这能为您解决一切,即使您不随意询问。

  

W / RepoOperation:/ Users / mnum处的updateChildren失败:DatabaseError:   路径W / Exception中的令牌无效:   com.google.firebase.database.DatabaseException:Firebase数据库   错误:路径中的令牌无效           com.google.firebase.database.DatabaseError.toException(com.google.firebase:firebase-database @@ 17.0.0:234)           com.google.firebase.database.core.utilities.Utilities $ 1.onComplete(com.google.firebase:firebase-database @@ 17.0.0:243)           com.google.firebase.database.core.Repo $ 6.run(com.google.firebase:firebase-database @@ 17.0.0:404)           在android.os.Handler.handleCallback(Handler.java:751)           在android.os.Handler.dispatchMessage(Handler.java:95)           在android.os.Looper.loop(Looper.java:154)           在android.app.ActivityThread.main(ActivityThread.java:6196)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:888)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)

private void AddAcc(final String nam, final String mnum)
  {
    final DatabaseReference Rootref;
    Rootref= FirebaseDatabase.getInstance().getReference();

    Rootref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            if (!(dataSnapshot.child("Users").child(mnum).exists())) {
                HashMap<String, Object> userDataMap = new HashMap<>();
                userDataMap.put("Name", nam);
                userDataMap.put("Phone No.", mnum); //Change "Phone No." to "Phone No"
                Rootref.child("Users").child(mnum).updateChildren(userDataMap)
                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {

                                if (task.isSuccessful()) {
                                    Toast.makeText(SignUp.this, "Data Stored", Toast.LENGTH_SHORT).show();
                                    loadingBar.dismiss();
                                } else {
                                    Toast.makeText(SignUp.this, "Network Error", Toast.LENGTH_SHORT).show();
                                    loadingBar.dismiss();
                                }
                            }
                        });


            } else {
                loadingBar.dismiss();
                Toast.makeText(SignUp.this, "Number is Already Present", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}