每次实施
时,我都会收到堆栈跟踪错误GenericTypeIndicator<List<String>> t = new GenericTypeIndicator<List<String>>() {
};
GenericTypeIndicator<List<String>> t2 = new GenericTypeIndicator<List<String>>() {};
List<String> namelist = snapshot.child("Driver name").getValue(t);
List<String> carlist = snapshot.child("Drivers car").getValue(t2);
if (namelist!=null & carlist!=null){
for(int wee=0;wee<namelist.size();wee++){
driver.add(new Driver(namelist.get(wee).toString(),carlist.get(wee).toString()));
}
我发送到firebase的数据存储为:
"DriversList" : {
"Driver name" : {
"-Js6OJniEqVZAYSNhqbB" : "gvvb",
"-Js7GjPMGNOLurrKk9rC" : "iggvv",
"-Js7GlUlCwIoljJxpFbZ" : "iiggv",
"-Js7GnBCRogiFUfn-ncZ" : "ihccvv",
"-Js7GqGxH2vLYK5tsjjP" : "hhvv",
"-Js7Gs80AoB5eeWci6jW" : "hhgc",
"-Js7Gtm6AGh_QaTUK6fQ" : "ugghh",
"-Js7GyomOIzjtVo3-PVg" : "tyygff",
"-JsPxEhf4tx7O4v7JWMc" : "",
"-JsQBoMqVBF4SzOHF3WA" : "rr",
"-JsQC6TuGqHrBBkdalik" : "eed"
},
"Drivers availability" : {
"-Js6OJozSuUvj5JnuNnW" : "No",
"-Js7GjQ_5YgmnEYKWHrL" : "No",
"-Js7GlVm7onoQd59Yyfq" : "No",
"-Js7GnDPdjseoRVjgwSn" : "No",
"-Js7GqIYy4VSEqu9qZfC" : "No",
"-Js7Gs8VP5mp5bOR1pxR" : "No",
"-Js7Gtmfv2M2p201k3Ra" : "No",
"-Js7Gyvf8JkGwBlh8ZLu" : "No",
"-JsPxEly9iGncA4VWvn2" : "No",
"-JsQBoOE6AbGy1VMBSWp" : "No",
"-JsQC6wGzBXBMz76IXue" : "No"
},
"Drivers car" : {
"-Js6OJoyDlc4DR0IQhJr" : "gvgh",
"-Js7GjQZ6Sb7AOooABt3" : "nbcgg",
"-Js7GlVajAA1B5PeBnn2" : "nvvuuu",
"-Js7GnDOCiWM874f6iL6" : "",
"-Js7GqIYy4VSEqu9qZfB" : "nbcxx",
"-Js7Gs8VP5mp5bOR1pxQ" : "hbccf",
"-Js7Gtmfv2M2p201k3R_" : "hvccf",
"-Js7Gyvf8JkGwBlh8ZLt" : "gghhhg",
"-JsPxElx9XXm0kVLFstN" : "",
"-JsQBoOE6AbGy1VMBSWo" : "dd",
"-JsQC6wF0zgLApv8SwJO" : "ff"
}
堆栈跟踪错误:
06-30 14:35:44.918 2736-2736/com.kate.teme E/AndroidRuntime﹕ FATAL EXCEPTION: main
com.firebase.client.FirebaseException: Failed to bounce to type
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:208)
at com.kate.teme.AdminContent$2.onDataChange(AdminContent.java:105)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: com.shaded.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: java.io.StringReader@42af1900; line: 1, column: 1]
at com.shaded.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:575)
at com.shaded.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:569)
at com.shaded.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.handleNonArray(StringCollectionDeserializer.java:218)
at com.shaded.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:166)
at com.shaded.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:156)
at com.shaded.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:19)
at com.shaded.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.shaded.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2041)
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:206)
at com.kate.teme.AdminContent$2.onDataChange(AdminContent.java:105)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:8)
查看您的数据结构:
"Driver name" : {
"-Js6OJniEqVZAYSNhqbB" : "gvvb",
"-Js7GjPMGNOLurrKk9rC" : "iggvv",
"-Js7GlUlCwIoljJxpFbZ" : "iiggv",
"-Js7GnBCRogiFUfn-ncZ" : "ihccvv",
"-Js7GqGxH2vLYK5tsjjP" : "hhvv",
"-Js7Gs80AoB5eeWci6jW" : "hhgc",
"-Js7Gtm6AGh_QaTUK6fQ" : "ugghh",
"-Js7GyomOIzjtVo3-PVg" : "tyygff",
"-JsPxEhf4tx7O4v7JWMc" : "",
"-JsQBoMqVBF4SzOHF3WA" : "rr",
"-JsQC6TuGqHrBBkdalik" : "eed"
}
您尝试将其反序列化为List<String>
。但它实际上是Map<String,String>
。所以:
@Override
public void onDataChange(DataSnapshot snapshot) {
GenericTypeIndicator<Map<String,String>> mapType = new GenericTypeIndicator<Map<String, String>>() { };
Map<String,String> namelist = snapshot.child("Driver name").getValue(mapType);
Map<String,String> carlist = snapshot.child("Drivers car").getValue(mapType);
if (namelist!=null & carlist!=null) {
for (String name: namelist.values()) {
System.out.println(name);
}
}
}
请注意,我不知道您是如何尝试匹配驾驶员和他们的汽车的。我强烈建议您在继续编码之前重新考虑此数据结构。
一种可能的结构:
"DriversList": {
"-Js6OJniEqVZAYSNhqbB" : {
"name": "puf",
"available": false,
"car": "Toyota RAV4"
},
"-Js7GjPMGNOLurrKk9rC" : {
"name": "manon",
"available": false,
"car": "Toyota Matrix"
},
...
}
要阅读/写这些内容,您需要Driver
课程:
public class Driver {
String name;
Boolean available;
String car;
public Driver() {}
public String getName() { return name; }
public Boolean getAvailable() { return available; }
public String getCar() { return car; }
}
然后:
public void onDataChange(DataSnapshot snapshot) {
GenericTypeIndicator<Map<String,Driver>> mapType = new GenericTypeIndicator<Map<String, Driver>>() { };
Map<String,Driver> drivers = snapshot.getValue(mapType);
if (drivers!=null) {
for (Driver driver: drivers.values()) {
System.out.println(driver.name);
}
}
}