'Asynchronous GET on REST with okhttp3

I am trying to perform an asynchronous GET-request on my openHAB-project. I have done it before and reused parts of my code to create a new Android app, but it is not working. In theory I want the state of the "GastSwitch"-item to be written into a String (gastSwitchState) to then be used as a trigger for opening a different activity. If the result of the request is "OFF" the app is supposed to keep running, but stay in the MainActivity.

When debugging it seems like the getGastSwitchState-method is jumped entirely after the enqeue-Method is called. Can someone explain to me, why my code seems to leave out half of the method?

I know that this way of doing it should work, but I can't find where I went wrong.

//connect with REST-API in openHAB :
        // GET Status GastSwitch: if Switch = "ON" go to MeetingActivity
        //Timer to GET the GastSwitch-status every 30 seconds:
        TimerTask gastSwitchTimerTask = new TimerTask() {
            public void run() {
                try {
                    getGastSwitchState("myURLforOpenHABGastSwitchState", new Callback() {      
                        @Override
                        public void getParameter(String string) {
                            if (gastSwitch.equals("ON")) {
                                Intent activityIntent = new Intent(getApplicationContext(), MeetingActivity.class);
                                startActivity(activityIntent);
                            }
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                    tvLog.setText(e.toString());
                }
            }
        };


// Timer for GETting the GastSwitch-state every 30 seconds
        long emergencyDelay = 1000 * 30 * 1;
        Timer gastSwitchTimer = new Timer();
        gastSwitchTimer.schedule(gastSwitchTimerTask, 0, emergencyDelay);

    }

    //Method for GETting the GastSwitch-state from REST-API:
    void getGastSwitchState(String url, final Callback callback) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        okhttp3.Request request = new okhttp3.Request.Builder()
                .url(url)
                .method("GET", null)
                .addHeader("AuthToken", "")
                .build();

        client.newCall(request)
                .enqueue(new okhttp3.Callback() {
                    @Override
                    public void onResponse(@NotNull okhttp3.Call call, @NotNull Response response) throws IOException {
                        if (response.isSuccessful()) {
                            final String res = response.body().string();
                            MainActivity.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    gastSwitch = res;
                                    tvLog.setText(gastSwitch);
                                    callback.getParameter(gastSwitch);
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(@NotNull okhttp3.Call call, @NotNull IOException e) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                            }
                        });
                    }

                });
    


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source