Firebase Rest Api 使用接口回调 return 从我的模型 类 读取数据 null

Firebase Rest Api reading data from my model classes using interface callback return null

我正在尝试 运行 java Restful API 中的 Get 方法。我尝试将我的模型 classes 命名为类似于我的实时 firebase。我试过使用接口回调,它已经打印到我的控制台,但它在我的邮递员中仍然 return null,

我的界面回调class

public interface CallBack {
    void onCallBack(DeviceTest value);

}

我的模型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 + '\'' +
                '}';
    }
}


FireBase 连接 class 和我使用的方法。

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


    }

我用来打印给邮递员的 Get 方法

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


这是我在邮递员那里得到的:

我在控制台中获得的内容:

我的问题是如何在我的控制台中将这个值 DeviceTest{LightSwitch='LIGHT', DoorSwitch='CLOSED'} 打印给我的邮递员?问题似乎出在我的 GET 方法中。

你是对的,抱歉误解了问题。 这是因为你 return deviceTest 就在它的实际值被解析之前。

您可能想使用 CompletableFuture。
尝试这样的事情:

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;

您可以拨打:

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

已解决 将我的 GET 方法编辑为此


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