Using android studio ( android )
how to Get data from Firebase database
using child value from unknown pushed key
as you can see in the picture friends
I'm trying to get data where
topicimage4 = 0
But I don't have the random key Firebase pushed
Firebase database
I searched alot about this Issue but I didn't found anything or
I found questions with no answers in this website
I tried also the following
myRef.child("*").orderByChild("topicimage4").equalTo("0")
myRef.child("?").orderByChild("topicimage4").equalTo("0")
myRef.orderByChild("topicimage4").equalTo("0")
myRef.orderByKey().orderByChild("topicimage4").equalTo("0")
UPDATE
here my Json tree as text
Is there any way to access to the topicimage4 child without having the Randrom key ? thnx
{
"-L5YppUZa3dGBvDUgnYi" : {
"paidmoney" : "400",
"paidmoneytype" : "1",
"topiccat" : "1",
"topiccon" : "0",
"topicemail" : "",
"topicimage1" : "null",
"topicimage2" : "null",
"topicimage3" : "null",
"topicimage4" : "0",
"topicimage5" : "21",
"topicname" : "مطلوب انسات للعمل ضمن صالون نسائي طريق عام جزين",
"topicphone" : "70645725",
"topicresouresname" : "",
"topicresouresurl" : "",
"topictext" : "في حال طلب منك الاتصال دولار اضافي على الدقيقة قم بإغلاق الخط \r\n\r\nمطلوب انسات للعمل ضمن صالون نسائي طريق عام جزين\r\n\r\n\r\nنحن نحاول قدر الامكان ضبط جميع انواع الوظائف المقدمة نشكر لكم اهتمامكم ومساعدتكم\r\n",
"topictimed" : "17",
"topictimeh" : "01",
"topictimei" : "57",
"topictimem" : "02",
"topictimey" : "18",
"userimage" : "0"
},
"-L5Yq6VJoOAN7dUKDO9O" : {
"paidmoney" : "400",
"paidmoneytype" : "1",
"topiccat" : "1",
"topiccon" : "0",
"topicemail" : "",
"topicimage1" : "null",
"topicimage2" : "null",
"topicimage3" : "null",
"topicimage4" : "0",
"topicimage5" : "0",
"topicname" : "يلزمنا انسه ذات مظهر لائق",
"topicphone" : "81614696",
"topicresouresname" : "",
"topicresouresurl" : "",
"topictext" : "في حال طلب منك الاتصال دولار اضافي على الدقيقة قم بإغلاق الخط \r\n\r\nيلزمنا انسه زات مظهر لائق للعمل في اكس برس عالاوتستراد بدوام كامل للمراجعه وتس اب التفاصيل موجوده كامله\r\n\r\n\r\n\r\nنحن نحاول قدر الامكان ضبط جميع انواع الوظائف المقدمة نشكر لكم اهتمامكم ومساعدتكم\r\n",
"topictimed" : "17",
"topictimeh" : "01",
"topictimei" : "58",
"topictimem" : "02",
"topictimey" : "18",
"userimage" : "0"
}
}
Related
Following is my firebase data structure
{
"ticketing" : {
"1476639649079-mahto" : {
"agentId" : 0,
"agent_status" : "1_Dispatched",
"approverId" : 0,
"contractorId" : 0,
"dateTime" : "16-10-2016 13:40 PM",
"priority" : "MEDIUM",
"requester" : {
"issue" : "issue 3",
"location" : "location 3",
"priority" : "Medium",
"shop" : "shop 3",
"site" : "site 3",
"userInfo" : {
"email" : "m#gmail.com",
"firstname" : "mahto",
"isLoggedin" : true,
"lastname" : "pancha",
"loggedin" : true,
"number" : "123123123",
"password" : "q",
"role" : "fieldagent",
"userid" : "2",
"username" : "q"
}
},
"requesterId" : 2,
"status" : "Incoming",
"ticketNumber" : "1476639649079-mahto"
},
"1476741838857-mahto" : {
"agentId" : 1,
"agent_status" : "1_Incoming",
"approverId" : 0,
"contractorId" : 0,
"dateTime" : "17-10-2016 18:03 PM",
"priority" : "HIGH",
"requester" : {
"issue" : "Description 3",
"location" : "Location 3",
"priority" : "HIGH",
"shop" : "shop 3",
"site" : "site 3",
"userInfo" : {
"email" : "m#gmail.com",
"firstname" : "mahto",
"isLoggedin" : true,
"lastname" : "pancha",
"loggedin" : true,
"number" : "123123123",
"password" : "q",
"role" : "fieldagent",
"userid" : "2",
"username" : "q"
}
},
"requesterId" : 2,
"status" : "Incoming",
"ticketNumber" : "1476741838857-mahto"
}
}
}
Database refrence
mDatabase = FirebaseDatabase.getInstance().getReference().child("ticketing");
mDatabase.orderByChild("agent_status").equalTo("1_Incoming");
and attached to recyclerview adapter
mAdapter = new FirebaseRecyclerAdapter<Ticket, AgentTicketHolder>(Ticket.class, R.layout.agent_ticket_cardview, AgentTicketHolder.class, mDatabase) { .. }
My expected result is that i will get only one node i.e 1476741838857-mahto which has agent_status value equal to 1_Incoming.
But i'am getting both the nodes in my recylerview.
When you call filtering methods, the Firebase SDK returns a new object for the resulting query. So you need to capture the reference to that object and pass that into the adapter:
mDatabase = FirebaseDatabase.getInstance().getReference().child("ticketing");
Query query = mDatabase.orderByChild("agent_status").equalTo("1_Incoming");
mAdapter = new FirebaseRecyclerAdapter<Ticket, AgentTicketHolder>
(Ticket.class, R.layout.agent_ticket_cardview, AgentTicketHolder.class, query) { .. }
I use the following the json for my android app.
[
{
"id" : "001",
"firstName" : "Mark",
"lastName" : "Mason",
"role" : "CEO",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
},
{
"teamName" : "iOS",
"members" : [
{
"id" : "002",
"firstName" : "Olly",
"lastName" : "Berry",
"role" : "iOS Team Lead",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png",
"teamLead" : true
},
{
"id" : "003",
"firstName" : "James",
"lastName" : "Frost",
"role" : "iOS Developer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
},
{
"id" : "004",
"firstName" : "Liam",
"lastName" : "Nichols",
"role" : "iOS Developer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
},
{
"id" : "005",
"firstName" : "Chris",
"lastName" : "Watson",
"role" : "iOS Developer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
},
{
"id" : "006",
"firstName" : "Richard",
"lastName" : "Turton",
"role" : "iOS Developer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
},
{
"id" : "007",
"firstName" : "Matt",
"lastName" : "Colliss",
"role" : "iOS Developer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
},
{
"id" : "008",
"firstName" : "David",
"lastName" : "Gibson",
"role" : "iOS Developer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
},
{
"id" : "009",
"firstName" : "Tom",
"lastName" : "Guy",
"role" : "iOS Developer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
},
{
"id" : "010",
"firstName" : "Rich",
"lastName" : "Hodgkins",
"role" : "iOS Developer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
}
]
},
{
"teamName" : "Android",
"members" : [{
"id" : "011",
"firstName" : "David",
"lastName" : "Branton",
"role" : "Android Team Lead",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png",
"teamLead" : true
},
{
"id" : "012",
"firstName" : "Dre",
"lastName" : "Pilipczuk",
"role" : "Android Developer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
},
{
"id" : "013",
"firstName" : "Ray",
"lastName" : "Britton",
"role" : "Android Developer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
},
{
"id" : "014",
"firstName" : "Charly",
"lastName" : "Murillo",
"role" : "Android Developer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
}
]
},
{
"teamName" : "Web",
"members" : [{
"id" : "015",
"firstName" : "Ryan",
"lastName" : "French",
"role" : "Web Team Lead",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png",
"teamLead" : true
},
{
"id" : "016",
"firstName" : "James",
"lastName" : "Ward",
"role" : "Web Developer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
},
{
"id" : "018",
"firstName" : "Adam",
"lastName" : "Smith",
"role" : "Web Developer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
},
{
"id" : "019",
"firstName" : "Leonard",
"lastName" : "Da Costa",
"role" : "Web Developer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
}
]
},
{
"teamName" : "Design",
"members" : [{
"id" : "020",
"firstName" : "Hannah",
"lastName" : "Tempest",
"role" : "Design Team Lead",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png",
"teamLead" : true
},
{
"id" : "021",
"firstName" : "Ellis",
"lastName" : "Reed",
"role" : "Designer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
},
{
"id" : "022",
"firstName" : "Pete",
"lastName" : "Horsham",
"role" : "Designer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
},
{
"id" : "023",
"firstName" : "Hemel",
"lastName" : "Dave",
"role" : "Designer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
},
{
"id" : "024",
"firstName" : "Hannah",
"lastName" : "Corke",
"role" : "Designer",
"profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png"
}
]
}
]
I display those data sucessfully in a recyclerview. However,I want to highlight the row of the lead photographer. The lead photographer object has a "teamLead" object,and the others don't.
So if I set it to true.
for(int j=0;j<teamMembersArray.length();j++){
//The model class which contains the setters/getters
//in order to "deserialize" the JSON objects into string,int,boolean
//objects.
Model m = new Model();
JSONObject teamObject = teamMembersArray.getJSONObject(j);
m.setId(teamObject.getInt("id"));
//This line refers to ALL JSON OBJECTS!!! I want
//to read only those who had teamLead separately from
//the other objects.
if(teamObject.has("teamLead")){
m.setTeamLead(teamObject.getBoolean("teamLead"));
}else {
m.setId(teamObject.getInt("id"));
m.setProfileImageURL(teamObject.getString("profileImageURL"));
m.setFirstName(teamObject.getString("firstName"));
m.setLastName(teamObject.getString("lastName"));
m.setRole(teamObject.getString("role"));
//Finally I am adding the string objects into an ArrayList.
modelArrayList.add(m);
}
I get an exception saying that teamLeader JSONObject,doesn't exist. It is so confusing and messed up situation.
How can I fix that?
Thanks,
Theo.
exception saying that teamLead JSONObject,doesn't exist
Because teamLead key is not avalaible in all JSONObject's which is in teamMembersArray JSONArray.
So, to get it work add check for null before accessing value for teamLead key from JSONObject like:
if (teamObject.has("teamLead") && !teamObject.isNull("teamLead")) {
m.setTeamLead(teamObject.getBoolean("teamLead"));
}else{
m.setTeamLead(false); // set default value here
}
This question already has answers here:
Sending and Parsing JSON Objects in Android [closed]
(11 answers)
Closed 7 years ago.
I have a json+collection string as following:
//code formated
{ "collection" :
{
"version" : "1.0",
"href" : "http://example.org/friends/",
"links" : [
{"rel" : "feed", "href" : "http://example.org/friends/rss"}
],
"items" : [
{
"href" : "http://example.org/friends/jdoe",
"data" : [
{"name" : "full-name", "value" : "J. Doe", "prompt" : "Full Name"},
{"name" : "email", "value" : "jdoe#example.org", "prompt" : "Email"}
],
"links" : [
{"rel" : "blog", "href" : "http://examples.org/blogs/jdoe", "prompt" : "Blog"},
{"rel" : "avatar", "href" : "http://examples.org/images/jdoe", "prompt" : "Avatar", "render" : "image"}
]
},
{
"href" : "http://example.org/friends/msmith",
"data" : [
{"name" : "full-name", "value" : "M. Smith", "prompt" : "Full Name"},
{"name" : "email", "value" : "msmith#example.org", "prompt" : "Email"}
],
"links" : [
{"rel" : "blog", "href" : "http://examples.org/blogs/msmith", "prompt" : "Blog"},
{"rel" : "avatar", "href" : "http://examples.org/images/msmith", "prompt" : "Avatar", "render" : "image"}
]
},
{
"href" : "http://example.org/friends/rwilliams",
"data" : [
{"name" : "full-name", "value" : "R. Williams", "prompt" : "Full Name"},
{"name" : "email", "value" : "rwilliams#example.org", "prompt" : "Email"}
],
"links" : [
{"rel" : "blog", "href" : "http://examples.org/blogs/rwilliams", "prompt" : "Blog"},
{"rel" : "avatar", "href" : "http://examples.org/images/rwilliams", "prompt" : "Avatar", "render" : "image"}
]
}
],
"queries" : [
{"rel" : "search", "href" : "http://example.org/friends/search", "prompt" : "Search",
"data" : [
{"name" : "search", "value" : ""}
]
}
],
"template" : {
"data" : [
{"name" : "full-name", "value" : "", "prompt" : "Full Name"},
{"name" : "email", "value" : "", "prompt" : "Email"},
{"name" : "blog", "value" : "", "prompt" : "Blog"},
{"name" : "avatar", "value" : "", "prompt" : "Avatar"}
]
}
}
}
sample linK: http://amundsen.com/media-types/collection/examples/
Can you please help me on how to decode this message. I have to read items , queries into my code and also version etc. I have to do this on android. I am not able to get how to use JSONObject to fetch the items, queries into my code.
For the array objects use JSONArray. Method arr.length() returns elements count, arr.get<type>(position) or just arr.get(position) for getting element.
you may use:
import org.json.JSONException;
import org.json.JSONObject;
in the following way:
try {
JSONObject myObj = new JSONObject("your json string");
} catch (JSONException e) {
e.printStackTrace();
}
I'm a beginner in android development ... i want to know how to create a json structure, with using arraylists and hashmaps ... you can find the structure as follows ...
NOTE: I don't want to use JSONArray or JSONObject
[
[
{
"blog_owner" : "user",
"icalIssueEventID" : "",
"updated_date" : "2014-02-17 07:05:53 +0000",
"icalEventID" : "",
"blog_stage" : "Cancelled",
"blog_ID" : "GMpk20140206160708281",
"created_date" : "2014-02-06 10:37:38 +0000",
"emailFlag" : false
}
],
[
{
"blog_iNotes_ID" : "",
"blog_name" : "",
"blog_status" : "",
"blog_duration" : ""
},
{
"pid" : "0",
"isOpen" : true,
"id" : "R1",
"level" : 0,
"value" : [
{
"level" : 0,
"id" : "MA20140206181038936",
"type" : "meetingAgenda",
"isOpen" : true,
"value" : [
{
"pid" : "MA20140206181038936",
"level" : 1,
"id" : "CN20140207211127374",
"isOpen" : false,
"value" : [
],
"line_number" : 13,
"keyItem" : "Fgh",
"isCategorySelected" : false
},
{
"line_number" : 14,
"level" : 1,
"id" : "CN20140217123343769",
"isOpen" : true,
"value" : [
],
"keyItem" : "Fghh",
"isCategorySelected" : false,
"pid" : "MA20140206181038936"
},
{
"pid" : "MA20140206181038936",
"level" : 1,
"id" : "ISS20140217123542229",
"isOpen" : true,
"type" : "Issues",
"value" : [
],
"type_KeyItem" : {
"tfImpact" : "",
"tvIssueDescription" : "",
"tfActionRequired" : "Ffg",
"tfResponsible" : "",
"targetDate" : "Month DD YYYY"
},
"keyItem" : "Issues",
"isCategorySelected" : true,
"line_number" : 15
}
],
"type_KeyItem" : {
"tfAgendaTitle" : "New agenda for magic minutes",
"lblDuration" : "00:00",
"tfAgendaName" : ""
},
"line_number" : 11,
"isCategorySelected" : false,
"pid" : "R1"
}
],
"isCategorySelected" : false,
"keyItem" : "root",
"line_number" : 10
}
],
[
]
]
You can use google-gson library for converting JSON to Java objects and vice-versa.
Some other helpful links are :
1) http://www.javacreed.com/simple-gson-example/
2) http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
Hope this will help you.
I'm trying to parse the following JSON string returned by httpResponse:
{ "message" : "List of top ten checkin Men.",
"success" : "True",
"top_ten_checkin_men" : [ { "City_name" : "Mumbai",
"is_block" : "no",
"is_favorite" : "no",
"is_friend" : "no",
"last_step_in" : "Pride Hotels",
"online" : "1",
"points" : "3800",
"request_pending" : "no",
"user_thumbphoto" : "",
"userage" : "26",
"userbodytype" : "3",
"userfname" : "John",
"usergender" : "Male",
"userid" : "191",
"userlname" : "Aarmani",
"userlookingfor" : "Female",
"useronlinestatus" : "Yes",
"userphoto" : ""
},
{ "City_name" : "New York",
"is_block" : "no",
"is_favorite" : "no",
"is_friend" : "no",
"last_step_in" : "The House of MG",
"online" : "1",
"points" : "4450",
"request_pending" : "no",
"user_thumbphoto" : "http://anburaj.com/lockme/uploads/users/188/thumb_121840970.jpg",
"userage" : "31",
"userbodytype" : "3",
"userfname" : "Williams",
"usergender" : "Male",
"userid" : "188",
"userlname" : "Johens",
"userlookingfor" : "Female",
"useronlinestatus" : "Yes",
"userphoto" : "http://anburaj.com/lockme/uploads/users/188/121840970.jpg"
}
]
}
and so on.
What I should I take for jsonarray and what should be gone for jsonobject?
[] defines a JSONArray (for instance top_ten_checkin_men). {} defines a JSONObject.
It's worth to take the time to know about the JSON format here:
"[]" represent array and "{}" is an object
What you have here is a JSONObject with success and message fields and a JSONArray top_ten_checkin_men
So something like :
JSONObject json = new JSONObject(yourString);
String message = json.getString("message");
String success = json.getString("success");
JSONArray array = json.getJSONArray("top_ten_checkin_men");
should do what you want