Add data from Api response into Firebase Database in Android - java

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

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.

Related

Jackson/Gson get specified key from Json - Java

i have problem with get specified value from json. I need get Key value from json below, its start with array/list.
[
{
"Version": 1,
"Key": "353333_PC",
"Type": "PostalCode",
"Rank": 500,
"LocalizedName": "Suwalki",
"EnglishName": "Suwalki",
"PrimaryPostalCode": "16-400",
"Region": {
"ID": "EUR",
"LocalizedName": "Europe",
"EnglishName": "Europe"
},
"Country": {
"ID": "PL",
"LocalizedName": "Poland",
"EnglishName": "Poland"
}
}
]
I have tried code like this:
String content = parent.path("Key").asText();
return content;
but it returns empty string. Have u any idea how get this?

JSON key some time string and some time list value android retrofit [duplicate]

This question already has answers here:
How to handle Dynamic JSON in Retrofit?
(12 answers)
Closed 5 years ago.
Response 1:
{
"id": "85",
"email": "jack#test.com",
"profession": [
{
"category_id": "1",
"name": "ARTIST"
}
],
"genre": [
{
"category_id": "3",
"name": "ROCK"
}
],
"instruments": "No list has found",
}
Response 2:
{
"id": "85",
"email": "jack#test.com",
"profession": [{
"category_id": "1",
"name": "ARTIST"
}],
"genre": [{
"category_id": "3",
"name": "ROCK"
}],
"instruments": [{
"category_id": "3",
"name": "ROCK"
}],
}
In first response instruments key have a String value, In second response instruments have a array. create the pojo class for second response but some times i got first response also it move OnFailure . how can i handle in Retrofit.
Different types of json objects is not possible to parse in single request/response. So you should use this on following way.
if your instruments object is empty means don't set string like "No list has found", Instead of you should send like "instruments": [],
now instruments is list coming but size is 0(zero), that means there is no data, like "No list has found"
so your response should be like this
{
"id": "85",
"email": "jack#test.com",
"profession": [{
"category_id": "1",
"name": "ARTIST"
}],
"genre": [{
"category_id": "3",
"name": "ROCK"
}],
"instruments": [],
"other": [{
"category_id": "4",
"name": "Test"
}],
}
Validate the list size in local and process your way..
Hope this will help you :)...

JSONObject["featured_image"] is not a String

Hi all I am getting a following json from Wordpress rest APi Call
[
{
"ID": 248,
"post_title": "test post",
"post_author": "admin",
"excerpt": "",
"url": "http://localhost/wp-learning/?p=248",
"permalink": "http://192.168.10.31/wp-learning/test-post/",
"post_date": "2017-08-07 09:15:57",
"comment_count": "2",
"category": [
{
"term_id": 1,
"name": "Uncategorized",
"slug": "uncategorized",
"term_group": 0,
"term_taxonomy_id": 1,
"taxonomy": "category",
"description": "",
"parent": 0,
"count": 1,
"filter": "raw",
"cat_ID": 1,
"category_count": 1,
"category_description": "",
"cat_name": "Uncategorized",
"category_nicename": "uncategorized",
"category_parent": 0
}
],
"views": "4",
"featured_image": false
},
{
"ID": 247,
"post_title": "The Coolest Vanity Apps for You and Your Girls",
"post_author": "admin",
"excerpt": "",
"url": "http://td_uid_79_598817d2cebf2",
"permalink": "http://192.168.10.31/wp-learning/the-coolest-vanity-apps-for-you-and-your-girls/",
"post_date": "2017-08-07 07:33:38",
"comment_count": "0",
"category": [
{
"term_id": 15,
"name": "Beauty",
"slug": "beauty",
"term_group": 0,
"term_taxonomy_id": 15,
"taxonomy": "category",
"description": "On each category you can set a Category template style, a Top post style (grids) and a module type for article listing.",
"parent": 14,
"count": 17,
"filter": "raw",
"cat_ID": 15,
"category_count": 17,
"category_description": "On each category you can set a Category template style, a Top post style (grids) and a module type for article listing.",
"cat_name": "Beauty",
"category_nicename": "beauty",
"category_parent": 14
}
],
"views": "0",
"featured_image": "http://192.168.10.31/wp-learning/wp-content/uploads/2017/08/3.jpg"
}
]
I am problem in fetching the tag name "featured_image". what type of datatype is it? i tried
jsonObj.getString("featured_image");
for this it gave me error JSONObject["featured_image"] is not a String.
also
resultsObj.getJSONObject("featured_image").toString()
for this it gave me error JSONObject["featured_image"] is not a JSONObject.
You are doing resultsObj.getJSONObject("featured_image").toString() which asks to get a JSON Object out of the Key/Value at 'featured_image'.
But we can see that the value is a string, or a bool:
"featured_image": "http://192.168.10.31/wp-learning/wp-content/uploads/2017/08/3.jpg"
"featured_image": false
So you should not call getJSONObject() on this key, at all.
Based on your JSON Structure, it looks like the "featured_image" is of boolean type, it is not string or object.
I think its inner json problem
try like this :
JSONObject category = jsonObj.getJSONObject("category");
category.getJSONObject("featured_image");
You can use following code, I didn't tested but that can be helpful in determining the data type and based on the type you can decide which method needs to be called.
So use:
JsonValue type = resultsObj.get("featured_image").getValueType();
if (type.getValueType() == JsonValue.ValueType.TRUE || type.getValueType() == JsonValue.ValueType.FALSE){
// call boolean method
boolean bValue = resultsObj.get("featured_image").getAsBoolean()
}else{
//based on you type you can call respective method.
}
Refer following link:
JsonValueType
getValueType

Checking Keys in JSON Records using Java

"transaction": {
"id": 1,
"empid": "12345",
"details1": {
"name": "xyz",
"age": "30",
"sex": "M",
"Address": {
"Office": "office",
"Home": "Home"
}
},
"abcDetails": "asdf",
"mobile": 123455
},
I need to test if JSON record contains more then two keys(details, Address).
Then, I need to pass those key input to this line:
parserValue1 = parserValue.asObject().get("firstKey").asObject().get("secondKey");
Can anyone help me?
Many json parsers have a has("key") or contains("key") accessor.
Otherwise you will have to add a condition to check if get("") returns null, or turn your whole Json object into a map, where you do the same checks.

Elasticsearch 5.1 Bulk Action

I'm try to make a bulk update
Method: Post
Url: /customer/external/_bulk
Json Body:
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
Id 1 is updated but id 2 didnt update. I dont know why?
Response is here:
{
"took": 138,
"errors": false,
"items": [
{
"index": {
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 15,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false,
"status": 200
}
}
]
}
As #Val mentioned, you should be having the new line character \n at the end of the last line in your json body:
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }\n
as per mentioned in bulk_api. Hope it helps!

Categories