0

I'm struggling a bit with a multithreaded problem. I need to send a request via sendRESTRequest(jsonRequest), but I dont't want to block the UI thread so maskerPane.setVisible will be executed.

I could use a JavaFX Task but then I have to code my currentValueLabel.setText (etc) in that thread. But because I'm reusing the sendRESTRequest(jsonRequest) method I will blow up my code with a lot of useless lines.

Is it possible to execute sendRESTRequest on antoher thread, wait for the result of Unirest.post and use the returned HttpResponse jsonResponse for further processing?

Currently I'm using this code:

@FXML
protected void searchButtonAction() {
    maskerPane.setVisible(true);

    cardNumber = cardNumberTextField.getText();

    JSONObject jsonRequest = new JSONObject()
    .put("id", cardNumber)
    .put("trans", 20);


            //
            // How to put this asynchronus, but wait for the answer before continuing to System.out.println(loyaltyResponse.toString());
            //
    JSONObject loyaltyResponse = sendRESTRequest(jsonRequest);
            //
            //
            //

    System.out.println(loyaltyResponse.toString());

    currentValueLabel.setText(loyaltyResponse.getString("amount").replace(".", ",") + " Currency");
    maximumValueLabel.setText(loyaltyResponse.getString("balanceMax").replace(".", ",") + " Currency");

    maskerPane.setVisible(false);
}

private JSONObject sendRESTRequest(JSONObject jsonRequest) {
    HttpResponse<JsonNode> jsonResponse = null;
    try {
        jsonResponse = Unirest.post("http://myurl/")
        .header("accept", "application/json")
        .body(jsonRequest)
        .asJson();
    } catch (UnirestException e) {
        e.printStackTrace();
    }

    return jsonResponse.getBody().getObject();
}

Thanks for your help!

Chris
  • 307
  • 2
  • 19
  • Arrange for the GUI to listen to the `value` property of a notional `Task`, as shown [here](https://stackoverflow.com/a/44056730/230513) or [here](https://stackoverflow.com/a/45718846/230513). – trashgod Feb 01 '19 at 17:19

1 Answers1

0

Now I solved it via

@FXML
protected void searchButtonAction() {
    maskerPane.setVisible(true);

    cardNumber = cardNumberTextField.getText();

    JSONObject jsonRequest = new JSONObject()
    .put("id", cardNumber)
    .put("trans", 20);
    Task<JSONObject> jsonRequestTask = new Task<JSONObject>() {
        @Override
        public JSONObject call() {
            return sendRESTRequest(jsonRequest);
        }
    };

    jsonRequestTask.setOnSucceeded(event -> {
        JSONObject loyaltyResponse = jsonRequestTask.getValue();

        currentValueLabel.setText(loyaltyResponse.getString("amount").replace(".", ",") + " Currency");
        maximumValueLabel.setText(loyaltyResponse.getString("balanceMax").replace(".", ",") + " Currency");

        maskerPane.setVisible(false);
    }

    jsonRequestTask.setOnFailed(event -> {
        maskerPane.setVisible(false);
    });

    new Thread(jsonRequestTask).start();
}

private JSONObject sendRESTRequest(JSONObject jsonRequest) {
    HttpResponse<JsonNode> jsonResponse = null;
    try {
        jsonResponse = Unirest.post("http://myurl/")
        .header("accept", "application/json")
        .body(jsonRequest)
        .asJson();
    } catch (UnirestException e) {
        e.printStackTrace();
    }

    return jsonResponse.getBody().getObject();
}

Works fine. Thanks for your help.

Chris
  • 307
  • 2
  • 19