提问者:小点点

com.google无法将java. lang.String类型的对象转换为model.Firebase.FirebaseTripDataHolder类型


我正在尝试在Firebase数据库的以下层次结构中获取每个节点及其子节点的值。因此,我构建了以下对象模型作为第一种方法:

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;
}
}

然后,我做了一个for循环来获取DataSnapshot的子项,如下代码片段所示:

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();
}

但是,以下行返回了异常

com.google无法将java. lang.String类型的对象转换为model.Firebase.FirebaseTripDataHolder类型

我试图对stackoverflow进行一些研究,我得到了太多的解决方案,就像Alex Mamo在这里的回答一样,看起来是同一个问题,我尝试了他的两个解决方案,并使用了即将到来的第二种方法:

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);
}

但是,在第二种方法中,OnDataChange方法中的dataSnapShot看起来有数据,但文件为每个属性返回空值。此外,我不知道如何完成我的对象以检索我的主DataSnapShot的子节点,例如:*下图中的客户坐标,客户信息,客户坐标*:在此处输入图像描述

谁能帮忙解决这个问题谢谢

更新一(1)以获得更多说明,这是我的json格式来自DataSnapShot键中的Firebase:

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} }

当datasnapshot看起来像DataSnapshot{key=当前状态,value=Accept}在循环内部时更新二(2),如下所示:

for (DataSnapshot ds : dataSnapshot.getChildren()) {
    TripStatus = ds.child("currentStatus").getValue(String.class); 
}

TripStatus等于空

更新三(3)

我的Firebase数据库参考是:tripsApplication=FirebaseDatabase. getInstance("https://wasalny-6139e.firebaseio.com/").getApplication().child("旅行").child(FirebaseListenedTripID);

我在问我的Object是否合适,我需要捕获旅行节点中的每个节点和子节点值,就像我想获取客户坐标、客户信息和目的地坐标一样

更新四(4)我的FirebaseDatabase json格式而不是附加的图像:

{
  "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"
        }
      }
    }
  }
}

更新五(5)我已经检索了dataSnapShot对象中的所有旅行节点及其子节点,问题是如何将其解析为我的FirebaseTripDataHolder对象!?


共1个答案

匿名用户

没有找到您对FirebaseDatabase查询。尝试使用以下代码:

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;
    }
}