curl -X PUT -d '{ "1": { "name": "Hauptbahnhof Berlin", "city": "Berlin"}, "2": { "name": "Hackerscher Markt", "city": "Berlin"}, "3": { "name": "Hauptbahnhof Frankfurt", "city": "Frankfurt"} }' 'https://xxx.firebaseio.com/stations.json'
results in
which looks exported like:
{
"stations" : [ null, {
"city" : "Berlin",
"name" : "Hauptbahnhof Berlin"
}, {
"city" : "Berlin",
"name" : "Hackerscher Markt"
}, {
"city" : "Frankfurt",
"name" : "Hauptbahnhof Frankfurt"
} ]
}
Selecting the data has a null value now, why so?
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference stationsRef = database.getReference("stations");
stationsRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<Map<String, String>> value = (ArrayList<Map<String, String>>) dataSnapshot.getValue();
Log.d(TAG, "Value is: " + value.toString());
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
=> Value is: [null, {name=Hauptbahnhof Berlin, city=Berlin}, {name=Hackerscher Markt, city=Berlin}, {name=Hauptbahnhof Frankfurt,
city=Frankfurt}]
Firebase automatically adds index 0 as a null value, if not passed. So changing the insert to
curl -X PUT -d '{ "0": { "name": "Hauptbahnhof Berlin", "city": "Berlin"}, "1": { "name": "Hackerscher Markt", "city": "Berlin"}, "2": { "name": "Hauptbahnhof Frankfurt", "city": "Frankfurt"} }' 'https://xxx.firebaseio.com/stations.json'
fixes the problem.
Related
I want to sort my main array using a child array value. Output as below:
{
"status": true,
"statusCode": 100,
"message": "",
"data": {
"win": [
{
"id": "1",
"admin": [
{
"id": "38",
"name": "Admin 05",
"point": {
"id": "96",
"name": "96"
}
}
]
},
{
"id": "2",
"admin": [
{
"id": "39",
"name": "Admin 06",
"point": {
"id": "95",
"name": "95"
}
}
]
},
{
"id": "3",
"admin": [
{
"id": "26",
"name": "Admin 05",
"point": {
"id": "98",
"name": "98"
}
}
]
}
]
}
}
I want array as below but I am not able to do so.
id = 3 // first
id = 1 // second
id = 2 // third and so on.
I tried below:
Collections.sort(loWinArray, object : Comparator<Win> {
override fun compare(obj1: Win, obj2: Win): Int {
// ## Ascending order
return (obj2.admin.get(0).point!!.name.toInt() - obj1.admin.get(0).point!!.name.toInt()) // To compare string values
}
})
Can any one please help?
If you want it to be sorted by the point ID, largest first to smallest last, try this:
val sortedArray = loWinArray.sortedByDescending { item -> item.admin.first().id.toInt() }
Try this for ascending order what you have tried it will return descending order
Collections.sort(loWinArray, object : Comparator<Win> {
override fun compare(obj1: Win, obj2: Win): Int {
// ## Ascending order
return obj1.admin.get(0).point!!.name.toInt() - obj2.admin.get(0).point!!.name.toInt()
}
})
The app is a group messaging and polling app.
The following is my JSON tree. Please let me know if it is the right format. As to make a complex app easily read and write data.
I will be using android studio for the client side and will make push and put requests and want to iterate and read data ideally using a childEventAdded listner.
{
"Groups": {
"0": {
"name": "group1",
"members": {
"harsh": "true",
"akshay": "true",
"ninad": "true"
}
},
"1": {
"name": "friends",
"members": {
"harsh": "true",
"akshay": "true"
}
}
},
"Members": {
"0": {
"member": "akshay",
"phone": "8975629079",
"groups": {
"group1": "true",
"friends": "true"
}
},
"1": {
"member": "harsh",
"phone": "9986768678",
"groups": {
"group1": "true",
"friends": "true",
"new group": "true"
}
},
"2": {
"member": "ninad",
"phone": "8172669079",
"groups": {
"group1": "true",
"friends": "true"
}
}
},
"Messages":
{
"0" : {
"user": "997637364",
"date": "24-6-2017:13:30",
"messsagetxt": [ "hello", "how is everyone"],
"group id" : "new group"
},
"1": {
"user": "7768914250",
"date": "24-6-2017:13:35",
"messsagetxt": [ "hi "],
"group id" : "new group"
},
"2" : {
"user": "8808957364",
"date": "24-6-2017:13:40",
"messsagetxt": [ "thanks for adding", "how is everyone"],
"group id" : "friends"
}
},
"Events": {
"polls": {
"0": {
"pollName": "Which movie",
"date": "24-6-2017",
"location": "Esquare",
"pollans": {
"hindi": {
"8975629079": "true",
"9923101446": "true"
},
"Eng": {
"7736354748": "true",
"7789384958": "true"
}
}
},
"1": {
"pollName": "Which phone should i buy",
"date": "4-2-2017",
"location": "",
"pollans": {
"Samsung galaxy s8": {
"8975629079": "true",
"9923101446": "true",
"7784650192": "True"
},
"iPhone 7+": {
"7736354748": "true",
"7789384958": "true"
},
"Windows Lumia": {
"7736354748": "true"
}
}
}
}
}
}
I made a Firebase Realtime Database and integrated it with my Android app. However, I am not able to filter results based on a field.
This is my database:
{
"news": {
"15211": {
"cat_id": 3,
"cat_name": "ലോകം",
"description": "ഒരുപക്ഷെ ജിഹാദികൾ പാകിസ്താന്റെ ഭരണം അട്ടിമറിച്ചു ...",
"id": 15211,
"image": "http://d3il9y9grvji6c.cloudfront.net/57ee50db0321d.jpg",
"item_type": 1,
"time": "1475236130",
"title": " പാക്കിസ്ഥാന്റെ ന്യൂക്ലിയർ ചാവേറുകൾ ഇന്ത്യയെ ആക്രമിക്കുമോയെന്ന് ഹിലറിക്ക് പേടി",
"url": "http://www.manoramaonline.com/news/just-in/hillary-clinton-fears-nuclear-suicide-bombers-from-pakistan.html",
"url_text": "മനോരമ"
},
"15382": {
"cat_id": 4,
"cat_name": "കായികം",
"description": "ന്യൂസിലന്ഡിനെതിരായ രണ്ടാം ക്രിക്കറ്റ് ടെസ്റ്റില് ...",
"id": 15382,
"image": "http://d3il9y9grvji6c.cloudfront.net/57f0fda3abe93.jpg",
"item_type": 1,
"time": "1475411410",
"title": "ഈഡനില് ഇന്ത്യ വിജയം മണക്കുന്നു; ലീഡ് 339 ",
"url": "http://www.reporterlive.com/2016/10/02/302271.html",
"url_text": "റിപ്പോർട്ടർ"
}
}
}
The code I am using to get all items with cat_id = 3 is
DatabaseReference ref = database.getReference("news");
final Query queryRef = ref.orderByChild("cat_id").equalTo(3).limitToLast(10);
queryRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
...
}
...
}
However, this always returns no result. What could be the issue?
I'm using Firebase for Android and found that the orderByChild("key") method does not seem to work on an array; I'm trying to retrieve all award_yearentries, ordered by their year value.
This is what I've came up with:
mDatabase.child("awards").orderByChild("createdAt").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
final AwardType awardType = new AwardType();
final ArrayList<Award> awards = dataSnapshot.getValue(awardType);
}
});
Your help is greatly appreciated.
I've added the rules:
My structure like this:
{ "awards": [
{
"award_body": "2009 QR Master Design Awards",
"award_received": "Finalist for Bathrooms $30,000, Qualified Remodeler Magazine",
"createdAt": "2015-11-20T03:43:58.009Z",
"logo": {
"__type": "File",
"name": "tfss-b2a0130d-d9ac-46e6-a29f-c9d9db666909-qualirem1.png",
"url": "awards/thumbnails/tfss-b2a0130d-d9ac-46e6-a29f-c9d9db666909-qualirem1.png"
},
"objectId": "0cE0zA7ciS",
"updatedAt": "2015-11-20T03:46:57.472Z",
"year": {
"__type": "Pointer",
"className": "award_years",
"objectId": "Zp3oDtzpTY"
}
},
{
"award_body": "2014 NAHB",
"award_received": "Best of American Living Awards (BALA) Platinum Award Winner, Kitchen Remodel $100,001 \u0026 over",
"createdAt": "2015-11-20T05:36:55.359Z",
"logo": {
"__type": "File",
"name": "tfss-3cce2507-18fe-4aac-b039-54c343f27473-nahb.png",
"url": "awards/thumbnails/tfss-3cce2507-18fe-4aac-b039-54c343f27473-nahb.png"
},
"objectId": "1ckh7kjxfN",
"updatedAt": "2015-11-20T05:45:02.307Z",
"year": {
"__type": "Pointer",
"className": "award_years",
"objectId": "aL7crUC7Qc"
}
}]
}
I'm trying to parse this json bellow, into a object named AlbumResponse that have another two objects inside, Album and PaginationInfo. Retrofit version 2.0
[
{
"id": "6",
"name": "King Stays King",
"artist_name":"Timbaland",
"image":""
},
{
"id": "7",
"name": "East Atlanta Santa 2",
"artist_name":"Gucci Mane",
"image":""
},
{
"id": "8",
"name": "The cuban connect",
"artist_name":"Phophit",
"image":""
},
{
"id": "9",
"name": "Shmoney Keeps",
"artist_name":"Calling",
"image":""
},
{
"id": "10",
"name": "Cabin Fever 3",
"artist_name":"Wiz khalifa",
"image":""
}
],
{
"nextPage": "http://private-ede172-mymixtapez1.apiary-mock.com/features/page_3/",
"itemsTotal": 10,
"page": 2,
"pagerMax": 2
}
Album class
public class Album {
long id;
String name;
#SerializedName("artist_name")
String artistName;
String image;
}
PaginationInfo class
public class PaginationInfo {
int page;
int pagerMax;
int nextPage;
int itemsTotal;
}
AlbumResponse, that have both classes above inside, and Album is a List
public class AlbumResponse {
public List<Album> albums;
public PaginationInfo paginationInfo;
}
The request
Call<AlbumResponse> responseCall = albumService.features();
responseCall.enqueue(new Callback<AlbumResponse>() {
#Override
public void onResponse(Response<AlbumResponse> response, Retrofit retrofit) {
if(response.isSuccess()) {
AlbumResponse albumResponse = response.body();
PaginationInfo paginationInfo = albumResponse.getPaginationInfo();
}
System.out.println();
}
#Override
public void onFailure(Throwable t) {
System.out.println(t.getMessage());
}
});
Interface
public interface AlbumService {
#GET("/features/")
Call<AlbumResponse> features();
}
The problem is that im getting a Throwable that contains:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $`
Please, can someone help me, i not found any answers in stackoverflow. Thanks.
The error says: the Parser expected a JSON-Object but it reads a JSON-array. To fix it (if you control the server) you should change the JSON String to something like this:
{
"albums" : [
{
"id": "6",
"name": "King Stays King",
"artist_name":"Timbaland",
"image":""
},
{
"id": "7",
"name": "East Atlanta Santa 2",
"artist_name":"Gucci Mane",
"image":""
},
{
"id": "8",
"name": "The cuban connect",
"artist_name":"Phophit",
"image":""
},
{
"id": "9",
"name": "Shmoney Keeps",
"artist_name":"Calling",
"image":""
},
{
"id": "10",
"name": "Cabin Fever 3",
"artist_name":"Wiz khalifa",
"image":""
}
],
"paginationInfo" : {
"nextPage": "http://private-ede172-mymixtapez1.apiary-mock.com/features/page_3/",
"itemsTotal": 10,
"page": 2,
"pagerMax": 2
}
}
Now it's a JSON-Object and is conform to your Java class.
If you cannot change the JSON on the backend, I would take it as row response and parse the albums Array and the PaginationInfo separately using GSON or manually.
Btw. you must change the nextPage type from int to String in the PaginationInfo class
Your JSON have a trouble in initial declaration.
According json.org:
JSON is built on two structures:
• A collection of name/value pairs. In various languages, this is
realized as an object, record, struct, dictionary, hash table, keyed
list, or associative array.
• An ordered list of values. In most
languages, this is realized as an array, vector, list, or sequence.
Try update your JSON to:
"albums": [
{
"id": "6",
"name": "King Stays King",
"artist_name":"Timbaland",
"image":""
},
{
"id": "7",
"name": "East Atlanta Santa 2",
"artist_name":"Gucci Mane",
"image":""
},
{
"id": "8",
"name": "The cuban connect",
"artist_name":"Phophit",
"image":""
},
{
"id": "9",
"name": "Shmoney Keeps",
"artist_name":"Calling",
"image":""
},
{
"id": "10",
"name": "Cabin Fever 3",
"artist_name":"Wiz khalifa",
"image":""
}
],
"pagination_info":
{
"nextPage": "http://private-ede172-mymixtapez1.apiary-mock.com/features/page_3/",
"itemsTotal": 10,
"page": 2,
"pagerMax": 2
}