您好,我正在开发一个应用程序,以在实现该应用程序时获取其应用程序的联系人,我正在接收该应用程序以敬酒所有联系人,但是联系人不在Firebase数据库中使用。我尝试了很多,但仍然无法将其存储在Firebase中。为了检查它,我尝试举杯,并获得所有列表,但没有存储。 以下是代码
public ProgressDialog dialog;
public DatabaseReference db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FirebaseApp.initializeApp(this);
db = FirebaseDatabase.getInstance().getReference().child("contact-fetch-firebase");
setContentView(R.layout.activity_main);
dialog = new ProgressDialog(this);
dialog.setMessage("Uploading contacts...");
Cursor contacts = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,new String[]{
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
},
null,
null,
null
);
HashMap<String,Object> map = new HashMap<>();
if(contacts!=null) {
while(contacts.moveToNext()){
map.put(
contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)),
contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
);
}
contacts.close();
}
dialog.show();
Iterator i = map.keySet().iterator();
while(i.hasNext()) {
String key=(String)i.next();
Object value=(Object) map.get(key);
Toast.makeText(getBaseContext(), "Name: " + key + " Contact Number: " + value, Toast.LENGTH_LONG).show();
}
db.updateChildren(map)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
String key = db.child("user").push().getKey();
String value = db.child("key").push().getKey();
dialog.dismiss();
Toast.makeText(MainActivity.this, "Contacts uploaded successfully!", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
dialog.dismiss();
Log.w("MKN","Error: "+e.getMessage());
Toast.makeText(MainActivity.this, "Contacts upload failed.", Toast.LENGTH_SHORT).show();
}
});
}
}
答案 0 :(得分:1)
首先要考虑的是更改数据库的身份验证规则,
以下规则仅允许经过身份验证的用户读取或写入数据。
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
以下规则将允许每个人无需身份验证即可读取和写入数据。
{
"rules": {
".read": true,
".write": true
}
}
答案 1 :(得分:0)
尝试这种方式-
Iterator i = map.keySet().iterator();
while(i.hasNext()) {
String key=(String)i.next();
Object value=(Object) map.get(key);
db.updateChildren(map)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
String key = db.child("user").push().getKey();
String value = db.child("key").push().getKey();
dialog.dismiss();
Toast.makeText(MainActivity.this, "Contacts uploaded successfully!", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w("MKN","Error: "+e.getException());
}
});
}