Home > Software design >  Firebase Rest Api reading data from my model classes using interface callback return null
Firebase Rest Api reading data from my model classes using interface callback return null

Time:11-14

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. I have tried using interface callback, and it has printed to my console but it still return null in my postman,

My interface callback class

public interface CallBack {
    void onCallBack(DeviceTest value);

}

my model class

public class DeviceTest implements Serializable {
    private String LightSwitch;
    private String DoorSwitch;

    // empty COnstructor
    public DeviceTest() { }

    public DeviceTest(String lightSwitch, String doorSwitch) {
        this.LightSwitch = lightSwitch;
        this.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;
    }

    @Override
    public String toString() {
        return "DeviceTest{"  
                "LightSwitch='"   LightSwitch   '\''  
                ", DoorSwitch='"   DoorSwitch   '\''  
                '}';
    }
}


The FireBase Connection class and the method I use.

public static void  handleLight(CallBack callback) {
        FireBaseService fbs = null;
        fbs = new FireBaseService();
        device = new DeviceTest();
        mylist = new ArrayList<Map<String, Object>>();
        DatabaseReference ref = fbs.getDb()
                .getReference("/Devices/Lamp/Ambient");
        Map<String, Object> chemainChild = new HashMap<>();
        // chemainChild.put("server/user/",array);

        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

           callback.onCallBack(dataSnapshot.getValue(DeviceTest.class));
            }


            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

//mylist.add(device);


    }

The Get method i use to print to postman

  DeviceTest deviceTest = new DeviceTest();
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public DeviceTest getCustomers() {


        //System.out.println(device.toString());
        FireBaseService.handleLight(new CallBack() {

            @Override
            public void onCallBack(DeviceTest value) {

                // this one will print to my console
                System.out.println("Should work "   value.toString());

                // this value does not seem to be added to my deviceTest, likely a asynchronous issue again. 
                deviceTest = new DeviceTest(value.getLightSwitch(),value.getDoorSwitch());
            }

        });

        // this is here i get my null value
        return deviceTest ;
    }


This is what i got in my postman:

What i got in my console:

My question is how can I print this value DeviceTest{LightSwitch='LIGHT', DoorSwitch='CLOSED'} in my console to my postman? The issue seems to be in my GET Method.

CodePudding user response:

You're right, sorry for misunderstanding the question. It is because you return deviceTest immediately before its actually value was resolved.

You probably want to use a CompletableFuture.
Try something like this:

CompletableFuture<DeviceTest> deviceTestFut = new CompletableFuture<>();

 //System.out.println(device.toString());
    FireBaseService.handleLight(new CallBack() {

        @Override
        public void onCallBack(DeviceTest value) {

            // this one will print to my console
            System.out.println("Should work "   value.toString());

            // this value does not seem to be added to my deviceTest, likely a asynchronous issue again. 
            deviceTestFut.complete(new DeviceTest(value.getLightSwitch(),value.getDoorSwitch());
        })

    });

    // this is here i get my null value
    return deviceTestFut;

The you can call:

deviceTestFut.thenAccept(value -> System.out.println("Should work "   value.toString()););

CodePudding user response:

Solved Edit my GET Method to this


        CompletableFuture<DeviceTest> deviceTestFut = new CompletableFuture<>();
        //System.out.println(device.toString());

        FireBaseService.handleLight(new CallBack() {
                                               @Override
                                               public DeviceTest onCallBack(DeviceTest value) {
                                                   deviceTest = new DeviceTest(value.getLightSwitch(),value.getDoorSwitch());

                                                   System.out.println("Here is the value"    deviceTest.toString());

                                                  deviceTestFut.complete(new DeviceTest(value.getLightSwitch(),value.getDoorSwitch()));
                                                   return deviceTest;
                                               }
                                           });


                // this is here i get my null value
       deviceTestFut.thenAccept(value -> System.out.println("Should work "   value.toString()));
 return deviceTestFut.get();
    }
  • Related