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?
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()
}
})
I'm trying to use Retrofit to get an array from my API. I then need to grab certain objects from the array to use in different parts of my app. Using the below code, I can get a Successful response, but when I try to log the response I get this...
myapp.com.myapp.LocationsModel#9a0d130
I need the actual array to be returned so I can grab objects (Strings) from the array. I ave tried several different implementations from Google and Stack Overflow, and I can't get this to work for the life of me. Maybe I'm overlooking something simple. This is my first time using Retrofit instead of Volley. Thanks for any help. Here is my model from LocationsModel. I have also pasted the api response that I'm trying to receive and parse.
Model
#GET("locations")
Call<List<LocationsModel>> getLocations(#Header("authorization") String token);
Client
public void getContests() {
// SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String token = LoginActivity.authToken;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Interface.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Interface api = retrofit.create(Interface.class);
Log.v("sendtoken", token);
Call<List<LocationsModel>> call = api.getLocations(token);
call.enqueue(new Callback<List<LocationsModel>>() {
#Override
public void onResponse(Call<List<LocationsModel>> call, Response<List<LocationsModel>> response) {
int statusCode = response.code();
Log.d("Response Code", "Response Code: " + statusCode);
Log.d("LocArray", "arr: " + Arrays.toString(response.body().toArray()));
}
#Override
public void onFailure(Call<List<LocationsModel>> call, Throwable t) {
Log.e("response-failure", call.toString());
t.printStackTrace();
}
});
}
API Json Array Response
[
{
"how_it_works": [
"Test program"
],
"id": 242,
"business_id": 162,
"title": "Take 10% off",
"desc_short": "10% off",
"logo_url": "http://www.logourl.com",
"hashtag": "#Octo",
"confirm_message_title": "Confirm Message",
"rules": "
The rules
",
"confirm_message": "Confirmation",
"start_date": "2018-04-01T05:00:00.000Z",
"end_date": "2018-06-01T04:59:59.000Z",
"active": true,
"entry_count": 0,
"created_at": "2018-04-16T19:15:03.000Z",
"updated_at": "2018-04-16T19:15:57.000Z",
"deleted_at": null,
"locations": [
{
"id": 232,
"business_id": 162,
"name": "Trent Home",
"address": "123 Second Place",
"city": "Rochester",
"state": "OK",
"zipcode": "35854",
"latitude": 33.0785,
"longitude": -87.5819,
"radius": 280.14,
"created_at": "2018-04-16T19:15:40.000Z",
"updated_at": "2018-04-16T19:15:40.000Z",
"deleted_at": null,
"contest_locations": {
"created_at": "2018-04-16T19:15:48.000Z",
"updated_at": "2018-04-16T19:15:48.000Z",
"contest_id": 242,
"location_id": 232
},
"stickers": [
"http://www.stickerurl.com"
]
}
],
"entries": []
},
{
"how_it_works": [
"Take a photo, get a reward"
],
"id": 292,
"business_id": 162,
"title": "Take 15% off",
"desc_short": "15% off $10",
"logo_url": "https://www.logourl.com",
"hashtag": "#testAndroid",
"confirm_message_title": "15% off now",
"rules": "
rules here
",
"confirm_message": "Complete good job",
"start_date": "2018-04-09T05:00:00.000Z",
"end_date": "2018-05-29T04:59:59.000Z",
"active": true,
"entry_count": 0,
"created_at": "2018-04-23T16:44:42.000Z",
"updated_at": "2018-04-23T16:44:56.000Z",
"deleted_at": null,
"locations": [
{
"id": 232,
"business_id": 162,
"name": "Trent Home",
"address": "123 Main St",
"city": "Tuls",
"state": "Ny",
"zipcode": "35235",
"latitude": 33.0785,
"longitude": -87.5819,
"radius": 280.14,
"created_at": "2018-04-16T19:15:40.000Z",
"updated_at": "2018-04-16T19:15:40.000Z",
"deleted_at": null,
"contest_locations": {
"created_at": "2018-04-23T16:44:49.000Z",
"updated_at": "2018-04-23T16:44:49.000Z",
"contest_id": 292,
"location_id": 232
},
"stickers": []
}
],
"entries": []
}
]
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.
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
}