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!