jeetdeveloper
jeetdeveloper

Reputation: 191

Add data from Api response into Firebase Database in Android

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

"data": [{
        "id": 1,
        "full_name": "abc",
        "email": "[email protected]",
        "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": "[email protected]",
        "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": "[email protected]",
        "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

Upvotes: 0

Views: 549

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

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.

Upvotes: 2

Related Questions