2017-03-10 160 views
0

未寫入數據庫的Firebase對象。 什麼是錯誤。 。活動正在運行,但沒有記錄在Firebase數據庫中寫入的值。怎麼辦未寫入數據庫的Firebase對象

這是我的java文件

package com.shaan.homely; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.Toast; 
import com.google.firebase.database.ChildEventListener; 
import com.google.firebase.database.DataSnapshot; 
import com.google.firebase.database.DatabaseError; 
import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.FirebaseDatabase; 
import com.google.firebase.database.ValueEventListener; 
import com.shaan.homely.pojo.User; 

import java.util.HashMap; 
import java.util.Map; 

public class Empty extends AppCompatActivity { 

FirebaseDatabase firebaseDatabase; 
DatabaseReference myref; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_empty); 

    User user = new User("shaan","[email protected]","9718460374"); 
    myref = FirebaseDatabase.getInstance().getReference(); 

    String userId = myref.push().getKey(); 
    Map<String,User> data = new HashMap<String,User>(); 
    data.put(userId,user); 
    //Toast.makeText(getApplicationContext(),myref.toString(),Toast.LENGTH_LONG).show(); 
    // creating user object 
    myref.child("users").child(userId).setValue(data); 

    myref.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      User user = dataSnapshot.getValue(User.class); 
      Toast.makeText(getApplicationContext(),user.getEmail()+user.getName(),Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 
      Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show(); 
     } 
    }); 

    myref.addChildEventListener(new ChildEventListener() { 
     @Override 
     public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
      User user = dataSnapshot.getValue(User.class); 
      Toast.makeText(getApplicationContext(),user.getEmail()+user.getName(),Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onChildChanged(DataSnapshot dataSnapshot, String s) { 

     } 

     @Override 
     public void onChildRemoved(DataSnapshot dataSnapshot) { 

     } 

     @Override 
     public void onChildMoved(DataSnapshot dataSnapshot, String s) { 

     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 
      Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show(); 

     } 
    }); 

} 
} 

,這是我的用戶類

package com.shaan.homely.pojo; 

import com.google.firebase.database.IgnoreExtraProperties; 

@IgnoreExtraProperties 
public class User { 

public String name; 
public String email; 
public String mobile; 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

public String getMobile() { 
    return mobile; 
} 

public void setMobile(String mobile) { 
    this.mobile = mobile; 
} 


// Default constructor required for calls to 
// DataSnapshot.getValue(User.class) 
public User() { 
} 

public User(String name, String email, String mobile) { 
    this.name = name; 
    this.email = email; 
    this.mobile = mobile; 
} 
} 
+0

檢查'Firebase'規則,它通常有參數'「.WRITE」 :「auth!= null」'。你可以將它定義爲'「.write」:true'來未經授權地寫入數據庫。 –

+0

我已經定義了它.. –

回答

0

你推一個值根數據庫,然後尋找它在孩子的用戶」。您最初的參考必須指向孩子「用戶」,要創建新條目:

myref = FirebaseDatabase.getInstance().getReference().child("users"); 

String userId = myref.push().getKey(); 
Map<String,User> data = new HashMap<String,User>(); 
data.put(userId,user); 
//Toast.makeText(getApplicationContext(),myref.toString(),Toast.LENGTH_LONG).show(); 
// creating user object 
myref.child(userId).setValue(data); 

UPDATE:

您也正在力推一個HashMap,但隨後讀取用戶類。我建議你的ID添加到用戶類,而不是創建HashMap中:

public class User { 
    ... 
    private String userId; 

    public String getUserId() { 
     return userId; 
    } 

    public void setUserId(String userId) { 
     this.userId = userId; 
    } 
} 

在您的代碼:

String userId = myref.push().getKey(); 
user.setUserId(userId); 

// creating user object 
myref.child("users").child(userId).setValue(user); 
+0

你說了什麼。 addValueEventListener返回Null時,我打印吐司值? –

+0

我更新了我的答案。 –

+0

仍然沒有更新數據庫這是我的firebase數據庫快照https://ibb.co/nOiOmF –