簡體   English   中英

Firebase Rest Api 使用接口回調從我的模型類讀取數據返回 null

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

我正在嘗試在 java Restful API 中運行 Get 方法。 我嘗試將我的模型類命名為類似於我的實時 firebase。 我嘗試使用接口回調,它已打印到我的控制台,但它仍然在我的郵遞員中返回 null,

我的接口回調類

public interface CallBack {
    void onCallBack(DeviceTest value);

}

我的模型課

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 Connection 類和我使用的方法。

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 方法中。

你是對的,抱歉誤解了這個問題。 這是因為您在解析其實際值之前立即返回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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM