How to parse json data comming from drupal site to android app? - android

I am trying to send data from drupal website to android app in the form of json. i have created the link and now my link is returning data in this form.
i don't have any idea how to parse this formatted data to show in a list view in android app.
{
"nodes" : [
{
"node" : {
"title" : "Saksham Iron Store",
"Contact" : "09216630565",
"Category" : "Decoration & Hardware",
"City" : "Sangrur"
}
},
{
"node" : {
"title" : "Loaffer Hut Garments",
"Contact" : "8591697698",
"Category" : "Fashion & Garments",
"City" : "Sangrur"
}
},
{
"node" : {
"title" : "BCB English Academy and Visa Guidance",
"Contact" : "8699010091",
"Category" : "Education",
"City" : "Sangrur"
}
},
{
"node" : {
"title" : "Aggarwal Engineering Corporation",
"Contact" : "9417070197",
"Category" : "Decoration & Hardware",
"City" : "Sangrur"
}
},
{
"node" : {
"title" : "Inder Sain Paint and Decoration",
"Contact" : "9888821904",
"Category" : "Decoration & Hardware",
"City" : "Sangrur"
}
}
]
}
How can i parse it and show in listview in android app?

Assuming that the JSON syntax of your receiving data conforms to the standard model you can parse JSON in android with one of the libraries already present in the org.json package (eg. JSONArray for arrays).
I'd suggest you take a look at GSON library ( my personal preference, there are of course many others) for it makes it easier to focus on your business logic and not occupy yourself with parsing JSON.

Related

Getting filtered list values from all users in my firebase Realtime-Database where the list is a child of user

This is how my firebase real-time database looks like.
{
"users" : {
"PDFZ0QOoTxYbCyruOrbiA2y1n5O2" : {
"email" : "mark#outlook.com",
"name" : "Mark Evans",
"paymentMethods" : {
"-ML5oMsCnXgfBRCh7DdO" : {
"paymentProvider" : "Google Pay",
"phone" : "+915555555555",
"userName" : "Mark Evans"
},
"-ML5oRHklHHIK33NQowD" : {
"paymentProvider" : "BHIM",
"phone" : "+911111111111",
"userName" : "Chris Evans"
}
},
"uid" : "PDFZ0QOoTxYbCyruOrbiA2y1n5O2"
},
"epyBsLU0fYOT8uc0Bo698f5SRcO2" : {
"email" : "axle#gmail.com",
"name" : "Axle Blaze",
"paymentMethods" : {
"-ML5o5Zv_3ZixCIXWBqg" : {
"paymentProvider" : "Google Pay",
"phone" : "+918888888888",
"userName" : "Axle Blaze"
},
"-ML5o9pMNucaacdU0G3P" : {
"paymentProvider" : "BHIM",
"phone" : "+911111111111",
"userName" : "Bunny Blaze"
}
},
"uid" : "epyBsLU0fYOT8uc0Bo698f5SRcO2"
}
}
}
I need to get those payment options where phone number = "+91XXXXXXXXXX" no matter from which user payment option belongs to.
For example: If i need all payment options where phone == +911111111111
Result should be :
"-ML5oRHklHHIK33NQowD" : {
"paymentProvider" : "BHIM",
"phone" : "+911111111111",
"userName" : "Chris Evans"
},
"-ML5o9pMNucaacdU0G3P" : {
"paymentProvider" : "BHIM",
"phone" : "+911111111111",
"userName" : "Bunny Blaze"
}
I have to do this in Android but since querying should be similar for any platform so I need help with how the query should be structured.
Any kind of help is appreciated. Thanks in advance.
Firebase queries work on a flat list. The value you order/filter on must be in a fixed location under each direct child node of users.
In your current structure, you can:
Search across all users for direct properties of that user, such as their email or name.
Search across the payment methods of a specific users.
The query you want is not possible on your current data structure. If you want to allow a query across all payment methods of all users, you'll need to change (or augment) your data structure to (also) have a flat list of payment methods across all users.
Also see:
Firebase Query Double Nested
Firebase query if child of child contains a value

In Firebase database, how to keep data consistency when both push and update operations are needed?

Say I have the following database in Firebase, when a user send a new message to another user, it need to push a new message to "ChatThreads/hash(User1,User2)" and update both "UserChats/User1/User2" and "UserChats/User2/User1".
"ChatThreads" : {
"hash(User1,User2)" : {
"1" : {
"message" : "Hello",
"sender" : "User2",
"time" : 9835592
},
"2" : {
"message" : "hi",
"sender" : "User1",
"time" : 10000000
},
"3" : {
"message" : "I am boss",
"sender" : "User2",
"time" : 14835592
}
}
}
},
"UserChats" : {
"User1" : {
"User2" : {
"lastTime" : 14835592,
"latestMessage" : "I am boss"
}
},
"User2" : {
"User1" : {
"lastTime" : 14835592,
"latestMessage" : "I am boss"
}
}
}
How do I keep data consistency in the case?
In the document of Firebase, the method of creating a reference to multiple update's common parent and put all updates in a map was introduced. However, in this case, not only update but also push is needed.
When you call push() without any arguments, you get a unique key that's always generated on the client. (Push ids are always generated locally.) With that key, you can then construct the path to the location where you want to add the data you along with all the other data for that update.

Firebase, query specific keys from a large set of data

Any help is much appreciated. Thank you so much for your time.
Let us say, there is a large set of articles under Articles node.
"Articles" : {
"article1Key" : {
"articleAuthor" : "Author",
"articleFavByNo" : 21,
"articleKey" : "Key",
"articleName" : "Name",
"articlePostedOn" : "21/07/11",
"articleTopic" : "Topic"
},
"article2Key" : {
"articleAuthor" : "Author",
"articleFavByNo" : 21,
"articleKey" : "Key",
"articleName" : "Name",
"articlePostedOn" : "21/07/11",
"articleTopic" : "Topic"
},
...
"article10Key" : {
"articleAuthor" : "Author",
"articleFavByNo" : 21,
"articleKey" : "Key",
"articleName" : "Name",
"articlePostedOn" : "21/07/11",
"articleTopic" : "Topic"
}
The articles are posted by some authors. Registered user can browse through the articles, and like them. The liked articles keys are stored in Users profile under favArticles node. Since, the article contains huge amount of data under it, only the key and name of the article is stored under User profile.
"Users" : {
"ZtlIQ2d1qJT1XpmHuGxwFSwaiEy2" : {
"emailId" : "vs#gmail.com",
"favArticles" : {
"article1Key" : {
"name" : "article1"
},
"article7Key" : {
"name" : "article7"
},
"article4Key" : {
"name" : "article4"
}
},
"firstName" : "Vimala",
"image" : "default",
"lastName" : "Sridhar"
}
}
Let us say that the user has liked some 50 articles out of 1000. Now if I want to display the User favorite articles in a RecyclerView, how should I write my query to pick the specific articles from the article list?
Since you already keep a list of the favorites for each user, you'd just:
load that list of favorites
iterate over it
load the references article for each
If you're worried about the performance of this loop-and-load: Firebase loads all the articles in step 3 over the same connection, so the requests are pipelined. For reasonable numbers of articles, this is actually quite fast. See my answer here for more details: Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly

Firebase Android Database

I am making an app that should include chat. I would like to display conversations of the current user. I am not sure how to get only chats of the current user. This is my structure of database.
"chats" : {
"-KWYiJiFwE6miPNkKPe0" : {
"chatPartner1" : {
"id" : "rtg3ru7zNYQQp9LA484VIG5rItl1",
"name" : "ghjg",
"photo" : ""
},
"chatPartner2" : {
"id" : "rtg3ru7bzwwewNYQwwewetegb",
"name" : "petra",
"photo" : ""
},
"id" : "-KWYiJiFwE6miPNkKPe0",
"lastMessage" : "dhjcd",
"timestamp" : 1479141836276
}
This json represents storing message for specified chats according to its id.
"messages" : {
"-KWYiJiFwE6miPNkKPe0" : {
"-KWYiJiPWR3n_YcvokCm" : {
"senderId" : "rtg3ru7zNYQQp9LA484VIG5rItl1",
"text" : "dhjcd",
"timestamp" : 1479141836276
}
}
I would need to check if the chatPartner 1 or 2 contains id of the current user. Or do you know about better way to store chats?
Thanks

Querying firebase with filtering data

So I have the Json in following format on firebase:
{
"30000000549656652" : {
"cast" : [ {
"src" : "https://encrypted-tbn1.gstatic.com/images",
"title" : "Hilary Duff (Samantha \"Sam\" Montgomery)"
}],
"channel_name" : "HBO",
"desc" : "After her .....",
"extras" : {
"initial" : "July 10, 2004 (Hollywood)",
"director" : "Mark Rosman",
"screenplay" : "Leigh Dunlap"
},
"genre" : "Thriller",
"lang" : "English",
"movie" : 1,
"prog_id" : 30000000549656652,
"prog_img" : "http://images."
"ratings" : {
"IMDb" : "5.9/10",
"Metacritic" : "25%",
"Rotten Tomatoes" : "11%"
},
"start" : 201607141800,
"stop" : 201607141700,
"title" : "Thrill A Cinderella Story",
"year" : "2004"
},
.....
}
I want to do the following in activity on my android app-
Query the node in which these JSON exist, then filter them such that i get all the results which have movies of one specific genre(e.g., Thriller) and the year is within five yrs less or five yrs more of the one i would pass in the query.
Like i would want to query to get all thriller movies whose "year" are between 2000 and 2009.
Can someone please help me to build such query for a json like this. Thanks!

Categories

Resources