简体   繁体   中英

Object missing data when retrieved from Firebase Database

I have an Attack object.

Attack.java

public class Attack implements Parcelable {
    private String pushId, website;
    private int networkType;
    private long timeMillis;
    private Map<String, String> hostInfo = new HashMap<>();
    private List<String> botIds = new ArrayList<>(); 

    public Attack() {
    }

    public Map<String, String> getHostInfo() {
        return hostInfo;
    }

    // getters/setters
}

The problem

I store an Attack at Firebase Database but when I am trying to retrieve them, its attack.getHostInfo() has one value instead of 4 as shown in the screenshot bellow.

Here is a screenshot of that instance from the console:

在此处输入图片说明

How listener is attached

I don't attach a ChildEventListener to an activity directly, but through a module called AttackRepository . An activity can use this class to upload / update / delete an Attack or to listen when an Attack was uploaded / updated / deleted .

Implementation of AttackRepository using Firebase

That's the FirebaseRepository class . Here is where the listener is attached:

public void startListenForChanges() {
    allAttacksRef.addChildEventListener(this);
}

So a stakeholder, a Fragment in my case, calls the above method and get's informed through AttackRepository when an Attack is uploaded, etc.

And, lastly, here is where how the stakeholder get's the data.

public void onChildAdded(@NonNull DataSnapshot dataSnapshot, String s) {
    Attack attack = dataSnapshot.getValue(Attack.class);
    repositoryListener.onAttackUpload(attack);
}

My fragment need to use the values of Attack.hostInfo but the other three are missing!

I am unable to reproduce your problem. Here's the minimal, standalone code that I've tried:

final DatabaseReference ref = FirebaseDatabase.getInstance().getReference("54752112");
ref.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        Attack attack = dataSnapshot.getValue(Attack.class);
        Log.i("Attack", attack.toString());
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String previousChildKey) { }
    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) { }
    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String previousChildKey) { }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
});

The only thing I changed about your original Attack class is that I added a toString() to it to print the properties:

 @Override
 public String toString() {
     return "{ pushId: '"+pushId+"', website: '"+website+"', networkType: "+networkType+
            ", timeMillis: "+timeMillis+", hostInfo: "+hostInfo.toString()+" }";
 }

And here's the JSON I used ( live link ):

{
  "-LZ0zS9LBuBxuW695B_i" : {
    "hostInfo" : {
      "extra_attack_host_uuid" : "cUgi890-Yrg",
      "extra_device_name" : "[Phone] Galaxy A3 (2016)",
      "extra_local_port" : "48790",
      "extra_mac_address" : "aa:81:95:c5:69:94"
    },
    "networkType" : 1,
    "pushId" : "-LZ0zS9LBuBxuW695B_i",
    "timeMillis" : 1550516609582,
    "website" : "http://www.sport24.gr"
  }
}

When I run the above, it prints:

I/Attack: { pushId: '-LZ0zS9LBuBxuW695B_i', website: ' http://www.sport24.gr ', networkType: 1, timeMillis: 1550516609582, hostInfo: {extra_mac_address=aa:81:95:c5:69:94, extra_device_name=[Phone] Galaxy A3 (2016), extra_local_port=48790, extra_attack_host_uuid=cUgi890-Yrg} }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM