需要多线程处理一个单一的请求并等待应答

我在多线程问题上有点挣扎。我需要通过 发送请求sendRESTRequest(jsonRequest),但我不想阻塞 UI 线程,所以maskerPane.setVisible会被执行。


我可以使用 JavaFX Task,但是我必须currentValueLabel.setText在那个线程中编写我的(等)。但是因为我正在重用该sendRESTRequest(jsonRequest)方法,所以我会用很多无用的行来破坏我的代码。


是否可以sendRESTRequest在另一个线程上执行,等待结果Unirest.post并使用返回HttpResponse jsonResponse的进行进一步处理?


目前我正在使用这段代码:


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

}

谢谢你的帮助!


当年话下
浏览 72回答 1
1回答

慕雪6442864

现在我通过@FXMLprotected void searchButtonAction() {&nbsp; &nbsp; maskerPane.setVisible(true);&nbsp; &nbsp; cardNumber = cardNumberTextField.getText();&nbsp; &nbsp; JSONObject jsonRequest = new JSONObject()&nbsp; &nbsp; .put("id", cardNumber)&nbsp; &nbsp; .put("trans", 20);&nbsp; &nbsp; Task<JSONObject> jsonRequestTask = new Task<JSONObject>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public JSONObject call() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return sendRESTRequest(jsonRequest);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; jsonRequestTask.setOnSucceeded(event -> {&nbsp; &nbsp; &nbsp; &nbsp; JSONObject loyaltyResponse = jsonRequestTask.getValue();&nbsp; &nbsp; &nbsp; &nbsp; currentValueLabel.setText(loyaltyResponse.getString("amount").replace(".", ",") + " Currency");&nbsp; &nbsp; &nbsp; &nbsp; maximumValueLabel.setText(loyaltyResponse.getString("balanceMax").replace(".", ",") + " Currency");&nbsp; &nbsp; &nbsp; &nbsp; maskerPane.setVisible(false);&nbsp; &nbsp; }&nbsp; &nbsp; jsonRequestTask.setOnFailed(event -> {&nbsp; &nbsp; &nbsp; &nbsp; maskerPane.setVisible(false);&nbsp; &nbsp; });&nbsp; &nbsp; new Thread(jsonRequestTask).start();}private JSONObject sendRESTRequest(JSONObject jsonRequest) {&nbsp; &nbsp; HttpResponse<JsonNode> jsonResponse = null;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; jsonResponse = Unirest.post("http://myurl/")&nbsp; &nbsp; &nbsp; &nbsp; .header("accept", "application/json")&nbsp; &nbsp; &nbsp; &nbsp; .body(jsonRequest)&nbsp; &nbsp; &nbsp; &nbsp; .asJson();&nbsp; &nbsp; } catch (UnirestException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; return jsonResponse.getBody().getObject();}工作正常。谢谢你的帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java