问题
I am trying to get value of every node and its subnodes in the following heirarchy on firebase database. So, I had built the following object Model as a First Approch:
public class FirebaseTripDataHolder {
String currentStatus,distanceLengthStr, driverId, durationStr, fareEstimate ;
public FirebaseTripDataHolder() {
}
public FirebaseTripDataHolder(String key, String tripStatus, String distanceSTR, String driverIDStr, String durationStr, String fareEstimate) {
currentStatus=tripStatus;
distanceLengthStr=distanceSTR;
driverId=driverIDStr;
this.durationStr=durationStr;
this.fareEstimate=fareEstimate;
}
public String getCurrentStatus() {
return currentStatus;
}
public void setCurrentStatus(String currentStatus) {
this.currentStatus = currentStatus;
}
public String getDistanceLengthStr() {
return distanceLengthStr;
}
public void setDistanceLengthStr(String distanceLengthStr) {
this.distanceLengthStr = distanceLengthStr;
}
public String getDriverId() {
return driverId;
}
public void setDriverId(String driverId) {
this.driverId = driverId;
}
public String getDurationStr() {
return durationStr;
}
public void setDurationStr(String durationStr) {
this.durationStr = durationStr;
}
public String getFareEstimate() {
return fareEstimate;
}
public void setFareEstimate(String fareEstimate) {
this.fareEstimate = fareEstimate;
}
}
Then, i made a for loop to get childrens of the DataSnapshot like the following snippet of code :
for (DataSnapshot ds : dataSnapshot.getChildren()) {
FirebaseTripDataHolder tripDataHolder= ds.getValue(FirebaseTripDataHolder.class);
TripStatus= tripDataHolder.getCurrentStatus();
DistanceSTR=tripDataHolder.getDistanceLengthStr();
DriverIDStr=tripDataHolder.getDriverId();
DurationStr=tripDataHolder.getDurationStr();
FareEstimate=tripDataHolder.getFareEstimate();
}
However, the following line had returned an exception
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type models.firebase.FirebaseTripDataHolder
I tried to make some research on stackoverflow, and i reached too many solutions like Alex Mamo's answer here, it looked the same problem, i tried both of his solutions and used the upcoming second Approach too :
for (DataSnapshot ds : dataSnapshot.getChildren()) {
KEY = ds.getKey();
TripStatus = ds.child("currentStatus").getValue(String.class);
DistanceSTR = ds.child("distanceLengthStr").getValue(String.class);
DriverIDStr = ds.child("driverId").getValue(String.class);
DurationStr = ds.child("durationStr").getValue(String.class);
FareEstimate = ds.child("fareEstimate").getValue(String.class);
}
But, in the second approach, dataSnapShot in OnDataChange method looked having data, but fileds are returning null values for each attribute. Also, i don't know how to complete my object to retrieve subnodes of my main DataSnapShot like : *customerCoordinates, customerInfo, distinationCoordinates * in the following image: enter image description here
May anyone help in that problem. Thanks
Update One(1) for more illustration, this is my json format coming from firebase in DataSnapShot key :
DataSnapshot { key = -Lv-5lEphq-F4TL1Q2_e, value = {distanceLength=12, customerCoordinates={address=Unnamed Road, lat=29.9774451, lng=31.2082394}, driverCoordinates={lat=29.977426, lng=31.212798}, fareEstimate=72, driverInfo={rate=5, name=driver29, deviceId=dYB0RihjjxY:APA91bFluyZ6iS0YDNKyY72TJl4Y9tx52MrVSwGKQkwJDuS9SVJ6XBlkpfCK1_bIIv675mY72cEr2en31qBPBzMOyaSE-zQHPBQJqdTDdkx-BZSyHZAeK5L1RBBrQRBuXPRsWiuJL2-h, mobile=01478529637}, durationStr=34 mins, distinationCoordinates={address=5 ش Ahmed Al Hasawi, lat=29.980184747972555, lng=31.25940430909395}, vehicleInfo={colorNames={ar=ازرق, en=Blue}, brandNames={ar=قيات, en=Fiat}, rideCategoryNames={ar=اقتصادي, en=Economy}, plateNo=789654, categoryNames={ar=تيبو, en=Tipo}, colorCode=#0000ff, typeNames={ar=موتوسكل, en=Bikes}}, currentStatus=Accept, customerInfo={name=mohamed, deviceId=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IkNVMTIxNjc4NzU2ODc3IiwiZW1haWwiOiJwcm9vZmVzc2VyQGdtYWlsLmNvbSIsInR5cGUiOiJDdXN0b21lciIsImlhdCI6MTU3NDEwMjE4MywiZXhwIjoxNTc2Njk0MTgzfQ.jqHNFmbLmOgS81y_vmpkVscsTDCrxBQiwfQPXuZLWTA, mobile=1097555350221}, driverId=DR472264941939, duration=34, distanceLengthStr=12.4 km} }
update two(2)
when datasnapshot looks like DataSnapshot { key = currentStatus, value = Accept }
inside for loop like that :
for (DataSnapshot ds : dataSnapshot.getChildren()) {
TripStatus = ds.child("currentStatus").getValue(String.class);
}
TripStatus equals null
Update Three(3)
my firebase Database reference is : tripsReference = FirebaseDatabase.getInstance("https://wasalny-6139e.firebaseio.com/").getReference().child("trips").child(firebaseListenedTripID);
I am asking whether my Object is the suitable one, i needed to catch every node and subnode values in the trips node like i want to get the customer coordinates, customer information, and destinationCoordinates
update Four (4) my firebaseDatabase json format instead of the attached image:
{
"drivers" : {
"DR117419391945" : {
".priority" : "stq6f8rfgx",
"g" : "stq6f8rfgx",
"l" : [ 30.0165321, 31.4097233 ]
},
"DR472264941939" : {
".priority" : "stq6fb27s5",
"g" : "stq6fb27s5",
"l" : [ 30.0166408, 31.4104284 ]
}
},
"trips" : {
"-Lv-BYEQPVDSV_AYAjPo" : {
"currentStatus" : "Accept",
"customerCoordinates" : {
"address" : "Unnamed Road",
"lat" : 29.9774637,
"lng" : 31.2082546
},
"customerInfo" : {
"deviceId" : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IkNVMTIxNjc4NzU2ODc3IiwiZW1haWwiOiJwcm9vZmVzc2VyQGdtYWlsLmNvbSIsInR5cGUiOiJDdXN0b21lciIsImlhdCI6MTU3NDEwMjE4MywiZXhwIjoxNTc2Njk0MTgzfQ.jqHNFmbLmOgS81y_vmpkVscsTDCrxBQiwfQPXuZLWTA",
"mobile" : "1097555350221",
"name" : "mohamed"
},
"distanceLength" : 12,
"distanceLengthStr" : "12.4 km",
"distinationCoordinates" : {
"address" : "5 ش Ahmed Al Hasawi",
"lat" : 29.980184747972555,
"lng" : 31.25940430909395
},
"driverCoordinates" : {
"lat" : 29.977426,
"lng" : 31.212798
},
"driverId" : "DR472264941939",
"driverInfo" : {
"deviceId" : "dYB0RihjjxY:APA91bFluyZ6iS0YDNKyY72TJl4Y9tx52MrVSwGKQkwJDuS9SVJ6XBlkpfCK1_bIIv675mY72cEr2en31qBPBzMOyaSE-zQHPBQJqdTDdkx-BZSyHZAeK5L1RBBrQRBuXPRsWiuJL2-h",
"mobile" : "01478529637",
"name" : "driver29",
"rate" : 5
},
"duration" : 34,
"durationStr" : "34 mins",
"fareEstimate" : 72,
"vehicleInfo" : {
"brandNames" : {
"ar" : "قيات",
"en" : "Fiat"
},
"categoryNames" : {
"ar" : "تيبو",
"en" : "Tipo"
},
"colorCode" : "#0000ff",
"colorNames" : {
"ar" : "ازرق",
"en" : "Blue"
},
"plateNo" : "789654",
"rideCategoryNames" : {
"ar" : "اقتصادي",
"en" : "Economy"
},
"typeNames" : {
"ar" : "موتوسكل",
"en" : "Bikes"
}
}
}
}
}
Update Five (5) I had retreived all trips node and its subnodes in the dataSnapShot object, the problem is how to parse it to my FirebaseTripDataHolder object!?
回答1:
Didn't find your query
to FirebaseDatabase
. Try using below code:
FirebaseDatabase database = FirebaseDatabase.getInstance();
database.getReference().child("trips").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
FirebaseTripDataHolder tripDataHolder = childSnapshot.getValue(FirebaseTripDataHolder.class);
String TripStatus= tripDataHolder.getCurrentStatus();
String DistanceSTR=tripDataHolder.getDistanceLengthStr();
String DriverIDStr=tripDataHolder.getDriverId();
String DurationStr=tripDataHolder.getDurationStr();
long FareEstimate=tripDataHolder.getFareEstimate();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
FirebaseTripDataHolder.java
public class FirebaseTripDataHolder {
String currentStatus,distanceLengthStr, driverId, durationStr;
long fareEstimate;
public FirebaseTripDataHolder() {
}
public FirebaseTripDataHolder(String key, String tripStatus, String distanceSTR, String driverIDStr, String durationStr, long fareEstimate) {
currentStatus=tripStatus;
distanceLengthStr=distanceSTR;
driverId=driverIDStr;
this.durationStr=durationStr;
this.fareEstimate=fareEstimate;
}
public String getCurrentStatus() {
return currentStatus;
}
public void setCurrentStatus(String currentStatus) {
this.currentStatus = currentStatus;
}
public String getDistanceLengthStr() {
return distanceLengthStr;
}
public void setDistanceLengthStr(String distanceLengthStr) {
this.distanceLengthStr = distanceLengthStr;
}
public String getDriverId() {
return driverId;
}
public void setDriverId(String driverId) {
this.driverId = driverId;
}
public String getDurationStr() {
return durationStr;
}
public void setDurationStr(String durationStr) {
this.durationStr = durationStr;
}
public long getFareEstimate() {
return fareEstimate;
}
public void setFareEstimate(long fareEstimate) {
this.fareEstimate = fareEstimate;
}
}
来源:https://stackoverflow.com/questions/59122830/com-google-firebase-database-databaseexception-cant-convert-object-of-type-jav