0

I am trying to run a Get method in java Restful API. I have tried naming my model classes similar to my real-time firebase that wasn't the issue.

This is my real-time firebase. reference is Devices/Lamp/Ambient

Here is my model class

 private String LightSwitch;
    private String DoorSwitch;

    // empty COnstructor
    public DeviceTest() { }

    public DeviceTest(String lightSwitch, String doorSwitch) {
        LightSwitch = lightSwitch;
        DoorSwitch = doorSwitch;
    }

    public String getLightSwitch() {
        return LightSwitch;
    }

    public void setLightSwitch(String lightSwitch) {
        LightSwitch = lightSwitch;
    }

    public String getDoorSwitch() {
        return DoorSwitch;
    }

    public void setDoorSwitch(String doorSwitch) {
        DoorSwitch = doorSwitch;
    }
}

Here is my readFirebase Method

public static List<DeviceTest> handleLight() {
        FireBaseService fbs = null;
        fbs = new FireBaseService();
      device = new DeviceTest();
        mylist = new ArrayList<>();
        DatabaseReference ref = fbs.getDb()
                .getReference("/Devices/Lamp");
        ref.child("Ambient")
                .addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        device = dataSnapshot.getValue(DeviceTest.class);
                        mylist = new ArrayList<>();
                        for (DataSnapshot unit : dataSnapshot.getChildren()) {
                            DeviceTest value = unit.getValue(DeviceTest.class);
                            mylist.add(value);

                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
//mylist.add(device);
        return mylist;
    }

Here is the GET method

@Path("/devices")
public class DeviceResource {
    
    DeviceService deviceService = new DeviceService();

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<DeviceTest> getCustomers(){
        return FireBaseService.handleLight();
    }

Here is the null value in my postman

ABAB
  • 25
  • 5
  • You cannot return the `mylist` as a result of a method. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this article, [How to read data from Firebase Realtime Database using get()?](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5). – Alex Mamo Nov 13 '21 at 12:16
  • I have tried using interface callback. That didn't solve the problem – ABAB Nov 13 '21 at 12:51
  • "That didn't solve the problem" doesn't provide enough information so we can help. So show us what you have tried by posting a new question, here on StackOverflow, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Nov 13 '21 at 12:52
  • Post a new question or can I edit this post to give further information? – ABAB Nov 13 '21 at 13:25
  • No, post a new question. – Alex Mamo Nov 13 '21 at 13:36

1 Answers1

0

It looks like you are returning your list before it is populated with data.

addValueEventListener is asynchronus so your method do not wait for results of it. onDataChange will be called when your data is ready but it can take a while, because it must download it from external source in network.

Have a look at this discussion: How to return DataSnapshot value as a result of a method?

Wojdakoman
  • 26
  • 2
  • Forgot to mention I have already tried using an interface class to get the value outside the method, but that didn't work either. – ABAB Nov 12 '21 at 18:21