我决定使用Android来查询数据库而不是使用云上的功能。
所以我想尝试运行查询
Query personQuery = myRef.orderByChild("mCalculateFaceSizeWidth")
.startAt(mCalculateFaceSizeWidth())
.limitToFirst(1);
personQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
Person person = dataSnapshot.getValue().getClass(Person.class);
}
....
这是我收到错误的地方
Person person = dataSnapshot.getValue().getClass(Person.class);
在getClass(Person.class)
上The error is: "in object can not be apply to java.lang.class<com.example.erang.facerecognition.Person>
这是我的Person类。
package com.example.erang.facerecognition;
import org.json.JSONObject;
import java.lang.reflect.Array;
/**
* Created by erang on 12-Jul-17.
*/
public class Person {
public String name;
public int age;
public String id;
public Array children;
public String address;
public String image;
public JSONObject faceDetails;
public Person() {
// Default constructor required for calls to DataSnapshot.getValue(Person.class)
}
public Person(String name,int age,String id,Array children,String address,String image,JSONObject faceDetails){
this.name = name;
this.age = age;
this.id = id;
this.children = children;
this.address = address;
this.image = image;
this.faceDetails = faceDetails;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public String getID(){
return id;
}
public Array getChildren(){
return children;
}
public String getAddress(){
return address;
}
public String getImage(){
return image;
}
public JSONObject getFaceDetails(){
return faceDetails;
}
}
这是我加载数据库的json文件。
{
"People":{
"EranGross":{
"name":"Eran Gross",
"age":42,
"id":"032208373",
"children":["Nadav","Amit"],
"address":"Aharon Boxer 38 Ness Ziona",
"image":"https://firebasestorage.googleapis.com/v0/b/facerecognition-29c9e.appspot.com/o/IMG_20170403_112227_641.jpg?alt=media&token=9523d69d-a61c-47b8-90b8-9f4b0c635d7d",
"faceDetails":{
"mCalculateFaceSizeHeight" : 121,
"mCalculateFaceSizeWidth" : 107,
"mCalculateLeftEyeBrowSizeHeight" : 31,
"mCalculateLeftEyeBrowSizeWidth" : 43,
"mCalculateLeftEyeSizeHeight" : 64,
"mCalculateLeftEyeSizeWidth" : 14,
"mCalculateMouthSizeHeight" : 24,
"mCalculateMouthSizeWidth" : 30,
"mCalculateNoseSizeHeight" : 43,
"mCalculateNoseSizeWidth" : 71,
"mCalculateRightEyeBrowSizeHeight" : 43,
"mCalculateRightEyeBrowSizeWidth" : 52,
"mCalculateRightEyeSizeWidth" : 14,
"mCalculatedRightEyeSizeHeight" : 36
}
},
"DavidWebb":{
"name":"David Webb",
"age":42,
"id":"414222333",
"children":["Nathan","Jason"],
"Address":"31 street San Jose",
"image":"https://firebasestorage.googleapis.com/v0/b/facerecognition-29c9e.appspot.com/o/IMG_20170403_112227_641.jpg?alt=media&token=9523d69d-a61c-47b8-90b8-9f4b0c635d7d",
"faceDetails":{
"mCalculateFaceSizeHeight" : 114,
"mCalculateFaceSizeWidth" : 103,
"mCalculateLeftEyeBrowSizeHeight" : 25,
"mCalculateLeftEyeBrowSizeWidth" : 22,
"mCalculateLeftEyeSizeHeight" : 50,
"mCalculateLeftEyeSizeWidth" : 14,
"mCalculateMouthSizeHeight" : 24,
"mCalculateMouthSizeWidth" : 20,
"mCalculateNoseSizeHeight" : 35,
"mCalculateNoseSizeWidth" : 68,
"mCalculateRightEyeBrowSizeHeight" : 35,
"mCalculateRightEyeBrowSizeWidth" : 45,
"mCalculateRightEyeSizeWidth" : 10,
"mCalculatedRightEyeSizeHeight" : 26
}
}
}
}
答案 0 :(得分:0)
尝试
public class RootPerson {
public Person person;
public class Person {
public String name;
public int age;
public String id;
public Array children;
public String address;
public String image;
public JSONObject faceDetails;
public Person() {
// Default constructor required for calls to DataSnapshot.getValue(Person.class)
}
public Person(String name,int age,String id,Array children,String address,String image,JSONObject faceDetails){
this.name = name;
this.age = age;
this.id = id;
this.children = children;
this.address = address;
this.image = image;
this.faceDetails = faceDetails;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public String getID(){
return id;
}
public Array getChildren(){
return children;
}
public String getAddress(){
return address;
}
public String getImage(){
return image;
}
public JSONObject getFaceDetails(){
return faceDetails;
}
}
}
和
Query personQuery = myRef.orderByChild("mCalculateFaceSizeWidth")
.startAt(mCalculateFaceSizeWidth())
.limitToFirst(1);
personQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
RootPerson person = dataSnapshot.getValue().getClass(RootPerson.class);
}
答案 1 :(得分:0)
更改此
Person person = dataSnapshot.getValue().getClass(Person.class);
到
Person person = dataSnapshot.getValue(Person.class);
您的方法不起作用的原因是getValue().getClass()
返回对象的运行时Class
而不是对象。您可以阅读更多相关信息here。