0

This I want to add data to firebase database through Volley API response in Android.

"data": [{
        "id": 1,
        "full_name": "abc",
        "email": "abc999@gmail.com",
        "country_code": "+91",
        "phone": 2147483647,
        "profile_pic": "",
        "type": 0,
        "status": 1,
        "reset_token": "",
        "verify_token": "$2y$10$YXCZ1yteimLatQnAszJTi.HOGDZrr9xjKJtIDNs3uagX3elFUlC.2",
        "created_at": "2019-05-07 07:53:29",
        "updated_at": "2019-05-08 12:57:45",
        "deleted_at": null
    }, {
        "id": 2,
        "full_name": "xyz",
        "email": "xyz@gm.com",
        "country_code": "+91",
        "phone": 2147483647,
        "profile_pic": "",
        "type": 0,
        "status": 1,
        "reset_token": null,
        "verify_token": "$2y$10$Dtk.BdqBgHFyGcpj9bHyI.JRPJlc90Qmhxx0Imm0Mzzd3x6QchDMi",
        "created_at": "2019-05-07 08:34:39",
        "updated_at": "2019-05-07 08:34:39",
        "deleted_at": null
    }, {
        "id": 3,
        "full_name": "abc",
        "email": "abc@gmail.com",
        "country_code": "091",
        "phone": 123456,
        "profile_pic": "",
        "type": 0,
        "status": 1,
        "reset_token": "$2y$10$mT9MqON6gMre0rKtoK0ON.VApZYBZP0PY55uM017Cz74E69qBILjm",
        "verify_token": "$2y$10$HMBteSyYTKZ3XgYviUdNORKOw1Bpan5m0UcqIyx3dZrYUsNajou",
        "created_at": "2019-05-07 08:47:29",
        "updated_at": "2019-05-17 05:55:00",
        "deleted_at": null
    }

]

Now I want firebase data should look like this

firebasedemo
    .
    + Users
       .
       . . name:"abc"

I am writing this line in Api call For loop Rootref= FirebaseDatabase.getInstance().getReference(); Rootref.child("Users").child("name").setValue(name);

But I am getting only one record in firebase not all

Any help will be highly appreciated

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
jeetdeveloper
  • 191
  • 1
  • 13

1 Answers1

2

Try the following:

DatabaseReference rootref = FirebaseDatabase.getInstance().getReference(); 
rootref.child("Users").push().setValue(name);

From the docs:

public DatabaseReference push ()

Create a reference to an auto-generated child location. The child key is generated client-side and incorporates an estimate of the server's time for sorting purposes. Locations generated on a single client will be sorted in the order that they are created, and will be sorted approximately in order across all clients.

Community
  • 1
  • 1
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • Hello I am placing this statement in for loop of API call. so the data added everytime the for loop is executed. can you pls guide me how do I check if my data exists or not. I am following this https://stackoverflow.com/a/41646772/9465933 but its not happening. @Peter Haddad – jeetdeveloper May 18 '19 at 07:09
  • @jeetdeveloper please ask it in a different question providing a snippet of code that you are using and @ me here so i can check your question – Peter Haddad May 18 '19 at 14:21