In my Android app I am using Google Firebase to store information in the database.
I need to iterate through the information, and below for-loop is being used
public void showDataLobReq(DataSnapshot dataSnapshot){
for(DataSnapshot ds : dataSnapshot.child("Lobby_Requests").getChildren()){
System.out.println("asdfasdfasdfasdfasdf"+ds.getValue());
game = ds.child(userID).child("game").getValue(String.class);
console = ds.child(userID).child("console").getValue(String.class);
mic = ds.child(userID).child("mic").getValue(String.class);
players = ds.child(userID).child("players").getValue(String.class);
}
}
You may notice that I put '.child("Lobby_Requests")' after dataSnapshot. this is because the dataSnapshot takes a snapshot of the whole database, so I must go into the subdirectory "Lobby_Requests" because that is where the information I need to iterate through is.
Putting this '.child()' in is being problematic.
I print to the console what the dataSnapshot contains in the first line of the for loop and with .child("Lobby_Requests") it is pulling information from the directory "Lobbies" in the actual database, which is a completely different directory.
Yet, when I remove the '.child()' completely it gives me a view of the whole database like it should. Why is it doing this?
Code for listener:
nRef = mFirebaseDatabase.getReference();
nRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
showDataLobReq(dataSnapshot);
} else {
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here is JSON:
{
"Games" : {
"Forza 6" : {
"Consoles" : {
"PC" : true,
"Xbox 1" : true,
"Xbox 360" : true
},
"FilePathName" : "forza6",
"Genres" : {
"Racing" : true
},
"Live Lobbies" : 0,
"Name" : "Forza 6"
},
"Minecraft" : {
"Consoles" : {
"PC" : true,
"Xbox 1" : true,
"Xbox 360" : true
},
"FilePathName" : "minecraft",
"Genres" : {
"Adventure" : true,
"Creation" : true,
"Open World" : true
},
"Live Lobbies" : 0,
"Name" : "Minecraft"
}
},
"Lobbies" : {
"Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
"Messages" : {
"-Kq6-1HsMvElEXZZyCIk" : {
"messageText" : "hey",
"messageTime" : 1501208519771,
"messageUser" : ""
}
},
"console" : "Origin",
"game" : "Minecraft",
"leader" : "Cd6lVd2XMUYoLH6b0xoHsrfXMud2",
"mic" : "Mic",
"note" : "2345",
"players" : "4"
},
"KUWH5f1TmYfO1O1wgCJLli3XZFi2" : {
"console" : "Steam",
"game" : "Forza 6",
"mic" : "No Mic",
"note" : "Hey Join Here!",
"players" : "2"
},
"hpWkq0D8clPReUetOq9Xtmc4V582" : {
"Messages" : {
"-Kq5a0kX305lFCRTSM_G" : {
"messageText" : "hello",
"messageTime" : 1501201701014,
"messageUser" : ""
},
"-Kq5asufOWQwtmyNJrQ7" : {
"messageText" : "hey",
"messageTime" : 1501201926941,
"messageUser" : ""
}
},
"console" : "Xbox One",
"game" : "Minecraft",
"leader" : "hpWkq0D8clPReUetOq9Xtmc4V582",
"mic" : "Mic",
"note" : "kjhg",
"players" : "4"
}
},
"Lobby_Requests" : {
"Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
"Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
"console" : "Xbox One",
"game" : "Forza 6",
"mic" : "Mic",
"players" : "5"
}
},
"KUWH5f1TmYfO1O1wgCJLli3XZFi2" : {
"KUWH5f1TmYfO1O1wgCJLli3XZFi2" : {
"console" : "Steam",
"game" : "Forza 6",
"mic" : "No Mic",
"players" : "2"
}
},
"hpWkq0D8clPReUetOq9Xtmc4V582" : {
"hpWkq0D8clPReUetOq9Xtmc4V582" : {
"console" : "Xbox One",
"game" : "Minecraft",
"mic" : "Mic",
"players" : "4"
},
"players" : "4"
}
},
"users" : {
"8cHrNCybwjO3PIUKxyOLiAqxJBv1" : {
"gamertag" : "thedylan",
"uname" : "thedood"
},
"Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
"gamertag" : "dmdylan",
"uname" : "ninja goat"
},
"KUWH5f1TmYfO1O1wgCJLli3XZFi2" : {
"gamertag" : "skaner",
"uname" : "asdf"
},
"YvYEIiCBUSYKTviVyWpLHdyDIFw1" : {
"gamertag" : "joejoe",
"uname" : "Jifflingly"
},
"ZmX9yIZ6MNguQa1S3MaYNcxfK2b2" : {
"gamertag" : "dmkaner",
"uname" : "dmkaner"
},
"hpWkq0D8clPReUetOq9Xtmc4V582" : {
"gamertag" : "dmkaner",
"uname" : "dmkaner"
},
"t21ncnuRmeV4F7RknETBisMrxS42" : {
"gamertag" : "asdf",
"uname" : "asdf"
}
}
}
The problem in your code is that you are pushing your data twice and there is no need for this.
"Lobby_Requests" : {
"Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
"Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : { //This is wrong
If you'll change the way in which you add data to the Firebase database by pushing that data only once, your code will work for fine. Your database should look like this:
"Lobby_Requests" : {
"Cd6lVd2XMUYoLH6b0xoHsrfXMud2" : {
"console" : "Xbox One",
"game" : "Forza 6",
"mic" : "Mic",
"players" : "5"
},
As you probably see, there is only one pushed key.
Related
I'm new in web development, I need your help & suggestion to make with easy ways vertical table in JSP or Javascript from the JSON data, like below image:
This is for table from database:
And here for the JSON data:
{
"_embedded" : {
"shipping" : [ {
"dayCd" : "1",
"shiftCd" : "1",
"qty" : "40",
"productName" : "Prod1",
"_links" : {
"self" : {
"href" : "http://127.0.0.1:8080/shipping/1001"
},
"shipping" : {
"href" : "http://127.0.0.1:8080/shipping/1001"
}
}
}, {
"dayCd" : "2",
"shiftCd" : "1",
"qty" : "40",
"productName" : "Prod1",
"_links" : {
"self" : {
"href" : "http://127.0.0.1:8080/shipping/1002"
},
"shipping" : {
"href" : "http://127.0.0.1:8080/shipping/1002"
}
}
}, {
"dayCd" : "3",
"shiftCd" : "1",
"qty" : "40",
"productName" : "Prod1",
"_links" : {
"self" : {
"href" : "http://127.0.0.1:8080/shipping/1003"
},
"shipping" : {
"href" : "http://127.0.0.1:8080/shipping/1003"
}
}
..........
UPDATE1: I still working on it but I can't solve it, maybe restructuring the database? I'll continue thinking but if someone has an idea it will be great to hear
Well I'm trying to get all current users friends and his own posts in a firebase query but I can't get them, always I tryed I only get one friend posts because the others are overwritten. Let me show you how my data base (JSON) is structured and how I want to display the post in the Android project Activity.
UserRef = FirebaseDatabase.getInstance().getReference().child("Users");
PostRef = FirebaseDatabase.getInstance().getReference().child("Post");
...
...
UserRef.child(currentUserID).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if(dataSnapshot.exists())
{
if(dataSnapshot.hasChild("Friends")){
for(DataSnapshot ds : dataSnapshot.child("Friends").getChildren()){
friendsID = ds.getKey(); //String with friendsID
DisplayAllUserPosts();
}
}...
...
private void DisplayAllUserPosts() {
Query SortPost = PostRef.orderByChild("uid").equalTo(friendsID);
FirebaseRecyclerOptions<Posts> options=new FirebaseRecyclerOptions.Builder<Posts>().setQuery(SortPost,Posts.class).build(); \\Posts class is just for getting data base Post nodes elements
FirebaseRecyclerAdapter<Posts, PostsViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Posts, PostsViewHolder>(options) {...
My Firebase Real Time Database is structured like this:
{
"Post" : {
"10-04-201918:32:09" : {
"counter" : 0,
"date" : "10-04-2019",
"fullname" : "Xuban Illarramendi",
"postimage" : "XXXX",
"profileimage" : "XXXX",
"status" : "",
"time" : "18:32:09",
"uid" : "FP1YJnlJBpfU5aSZlSAErLODWZl2"
},
"10-04-201921:10:06" : {
"counter" : 0,
"date" : "10-04-2019",
"fullname" : "Ander Lopez",
"postimage" : "XXXX",
"profileimage" : "XXXX",
"status" : "",
"time" : "21:10:06",
"uid" : "8wXFsw1pBRYXBVtChqY1Kqi14dy1"
}
},
"Users" : {
"8wXFsw1pBRYXBVtChqY1Kqi14dy1" : {
"Friends" : {
"FP1YJnlJBpfU5aSZlSAErLODWZl2" : {
"date" : "10-04-2019"
},
"sWQGtchFE1fvuSIfPb42ZIWVkbU2" : {
"date" : "10-04-2019"
}
},
"country" : "Spain",
"dob" : "none",
"fullname" : "Ander Lopez",
"gender" : "none",
"profileimage" : "XXXX",
"relationshipstatus" : "none",
"status" : "",
"username" : "anderlopz"
},
"FP1YJnlJBpfU5aSZlSAErLODWZl2" : {
"Friends" : {
"8wXFsw1pBRYXBVtChqY1Kqi14dy1" : {
"date" : "10-04-2019"
},
"sWQGtchFE1fvuSIfPb42ZIWVkbU2" : {
"date" : "10-04-2019"
}
},
"country" : "Spain",
"dob" : "none",
"fullname" : "Xuban Illarramendi",
"gender" : "none",
"profileimage" : "XXXX",
"relationshipstatus" : "none",
"status" : "",
"username" : "xuban"
},
"sWQGtchFE1fvuSIfPb42ZIWVkbU2" : {
"Friends" : {
"8wXFsw1pBRYXBVtChqY1Kqi14dy1" : {
"date" : "10-04-2019"
},
"FP1YJnlJBpfU5aSZlSAErLODWZl2" : {
"date" : "10-04-2019"
}
},
"country" : "Spain",
"dob" : "none",
"fullname" : "Unai Lopez",
"gender" : "none",
"profileimage" : "XXXX",
"relationshipstatus" : "none",
"status" : "",
"username" : "unailopz"
}
}
}
So my real problem is that he displays only the last friend posts, how can I make a query that contains all my friendsID without overwritting the previous friendsID?
I am building an REST API with Spring Boot. The question is really simple, how can I enable pagination for dependency request?
This how the results look for a non dependency request http://localhost:8080/dimensionAttributes:
{
"_embedded" : {
"dimensionAttribute" : [ {
"name" : "SollFreitag",
"description" : "Sollstunden Freitags",
"dataType" : "DEC",
"hasHistory" : true,
"hasTrigger" : null,
"allowInterface" : true,
"readOnly" : null,
"discrete" : null,
"mandatory" : false,
"_links" : {
"self" : {
"href" : "http://localhost:8080/dimensionAttributes/20"
},
"dimensionAttribute" : {
"href" : "http://localhost:8080/dimensionAttributes/20"
},
"dimension" : {
"href" : "http://localhost:8080/dimensionAttributes/20/dimension"
},
"dimensionAttributeValue" : {
"href" : "http://localhost:8080/dimensionAttributes/20/dimensionAttributeValue"
}
}
} ]
},
"_links" : {
"first" : {
"href" : "http://localhost:8080/dimensionAttributes?page=0&size=20"
},
"self" : {
"href" : "http://localhost:8080/dimensionAttributes{?page,size,sort}",
"templated" : true
},
"next" : {
"href" : "http://localhost:8080/dimensionAttributes?page=1&size=20"
},
"last" : {
"href" : "http://localhost:8080/dimensionAttributes?page=1&size=20"
},
"profile" : {
"href" : "http://localhost:8080/profile/dimensionAttributes"
},
"search" : {
"href" : "http://localhost:8080/dimensionAttributes/search"
}
},
"page" : {
"size" : 20,
"totalElements" : 35,
"totalPages" : 2,
"number" : 0
}
}
But running an dependency request (in this case its "dimensionAttributeValue" ) http://localhost:8080/dimensionAttributes/20/dimensionAttributeValue, the page section, as well as the next url and other links are missing.
{
"_embedded" : {
"dimensionAttributeValue" : [ {
"chrValue" : "Mihal, Matus",
"intValue" : null,
"decValue" : null,
"datValue" : null,
"startDate" : null,
"_links" : {
"self" : {
"href" : "http://localhost:8080/dimensionAttributeValues/18993"
},
"dimensionAttributeValue" : {
"href" : "http://localhost:8080/dimensionAttributeValues/18993"
},
"lstValue" : {
"href" : "http://localhost:8080/dimensionAttributeValues/18993/lstValue"
},
"dimensionAttribute" : {
"href" : "http://localhost:8080/dimensionAttributeValues/18993/dimensionAttribute"
},
"masterData" : {
"href" : "http://localhost:8080/dimensionAttributeValues/18993/masterData"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/dimensionAttributes/1/dimensionAttributeValue?page=0,&size=1"
}
}
}
I have simple Spring Boot application. My controller looks like this:
#RequestMapping(value = "/", method = RequestMethod.GET)
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
}
This code produce this json:
{
"_embedded" : {
"employees" : [ {
"firstName" : "firstName1",
"lastName" : "lastName1",
"_links" : {
"self" : {
"href" : "http://localhost:8080/employees/1"
},
"employee" : {
"href" : "http://localhost:8080/employees/1"
}
}
}, {
"firstName" : "firstName 1",
"lastName" : "lastName 1",
"_links" : {
"self" : {
"href" : "http://localhost:8080/employees/2"
},
"employee" : {
"href" : "http://localhost:8080/employees/2"
}
}
}, {
"firstName" : "firstName 3",
"lastName" : "lastName 3",
"_links" : {
"self" : {
"href" : "http://localhost:8080/employees/4"
},
"employee" : {
"href" : "http://localhost:8080/employees/4"
}
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/employees"
},
"profile" : {
"href" : "http://localhost:8080/profile/employees"
}
},
"page" : {
"size" : 20,
"totalElements" : 7,
"totalPages" : 1,
"number" : 0
}
}
But I need something like this:
[
{
"firstName" : "firstName1",
"lastName" : "lastName1",
},
{
"firstName" : "firstName2",
"lastName" : "lastName2",
},
]
The solution is removing spring-boot-starter-data-rest module.
How to get expected output below where OrderProjection uses ItemProjection to render Items using Spring Data REST
GET /orders/1?projection=with_items
Projections :
#Projection(name = "summary", types = Item.class)
public interface ItemProjection {
String getName();
}
#Projection(name = "with_item", types = Order.class)
public interface OrderProjection {
LocalDateTime getOrderedDate();
Status getStatus();
Set<ItemProjection> getItems(); // this is marshalling as Set<Item> (full Item graph)
}
Currently getting as output:
{
"status" : "PAYMENT_EXPECTED",
"orderedDate" : "2014-11-09T11:33:02.823",
"items" : [ {
"name" : "Java Chip",
"quantity" : 1,
"milk" : "SEMI",
"size" : "LARGE",
"price" : {
"currency" : "EUR",
"value" : 4.20
}
} ],
"_links" : {
"self" : {
"href" : "http://localhost:8080/orders/1{?projection}",
"templated" : true
},
"restbucks:items" : {
"href" : "http://localhost:8080/orders/1/items"
},
"curies" : [ {
"href" : "http://localhost:8080/alps/{rel}",
"name" : "restbucks",
"templated" : true
} ]
}
}
Expected Output:
{
"status" : "PAYMENT_EXPECTED",
"orderedDate" : "2014-11-09T11:33:02.823",
"items" : [ {
"name" : "Java Chip"
} ],
"_links" : {
"self" : {
"href" : "http://localhost:8080/orders/1{?projection}",
"templated" : true
},
"restbucks:items" : {
"href" : "http://localhost:8080/orders/1/items"
},
"curies" : [ {
"href" : "http://localhost:8080/alps/{rel}",
"name" : "restbucks",
"templated" : true
} ]
}
}
You're running into DATAREST-394 which has been fixed a few days a go and will be making it into 2.2.2 and 2.3 RC1. It's already available in the snapshots for said versions, feel free to give them a spin.