将数据从Javafx发送表单发送到HTTP POST请求

时间:2017-09-14 20:25:50

标签: java javafx

我使用Javafx Scene Builder 2.0创建了一个表单我的表单工作并且将变量设置为表单元素值。我还创建了一个PHP脚本,用于接收后期数据并将数据插入数据库。

我需要一些帮助,实际上通过http post将javafx表单数据发送到我的php脚本。

到目前为止,这是我的java代码。

Main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Orbis Cob Submit");
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}

Controller.java

package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.text.Text;
import java.io.IOException;

import java.io.IOException;
import java.util.Observable;

public class Controller {
    //Send Vars
    private String sendDataType;
    private String sendCobTarget;
    private String sendVendor;
    private String sendCobName;
    private String sendDealerID;
    private String sendJobType;
    private String sendStartDate;
    private String sendEndDate;
    private String sendAmount;
    private String sendCost;
    private String sendDataDescription;
    private DataBase db = new DataBase();

    //Data list for drop downs
    ObservableList<String> dataTypeList = FXCollections.observableArrayList("BK", "BR", "DB", "DEQ", "FI", "MB", "OB", "SAT", "SURN");
    ObservableList<String> cobTargetList = FXCollections.observableArrayList("Sales", "Services", "Both");
    ObservableList<String> vendorList = FXCollections.observableArrayList("SJO", "OCZ","Inhouse");
    ObservableList<String> jobTypeList = FXCollections.observableArrayList("Mail", "Digital");

    //Form Elements
    @FXML
    public ChoiceBox dataType;

    @FXML
    public ChoiceBox cobTarget;

    @FXML
    public ChoiceBox vendor;

    @FXML
    public Button button;

    @FXML
    public Text message;

    @FXML
    public TextField cobName;

    @FXML
    public TextField dealerID;

    @FXML
    public ChoiceBox jobType;

    @FXML
    public DatePicker startDate;

    @FXML
    public DatePicker endDate;

    @FXML
    public TextField sentAmount;

    @FXML
    public TextField cost;

    @FXML
    public TextField dataDescription;


    @FXML
    private  void initialize(){
        dataType.setItems(dataTypeList);
        cobTarget.setItems(cobTargetList);
        vendor.setItems(vendorList);
        jobType.setItems(jobTypeList);

         button.setOnAction(e -> {
             this.sendDataType = dataType.getValue().toString();
             this.sendCobTarget = cobTarget.getValue().toString();
             this.sendVendor = vendor.getValue().toString();
             this.sendCobName = cobName.getText();
             this.sendDealerID = dealerID.getText();
             this.sendJobType = jobType.getValue().toString();
             this.sendStartDate = startDate.getValue().toString();
             this.sendEndDate = endDate.getValue().toString();
             this.sendAmount = sentAmount.getText();
             this.sendCost = cost.getText();
             this.sendDataDescription = dataDescription.getText();

            //Send Field Data to HTTP POST REQUEST

        });
    }



}

1 个答案:

答案 0 :(得分:0)

您最好的选择是使用像这样的库,这样可以更轻松地发送HTTP(S)请求。

你这样使用它:

Unirest.post("http://httpbin.org/post")
    .field("sendDataType", dataType.getValue().toString())
    .field("sendCobTarget", cobTarget.getValue().toString())
    .field("sendDataType", dataType.getValue().toString())
    .field("sendCobTarget", cobTarget.getValue().toString())
    .field("sendVendor", vendor.getValue().toString())
    .field("sendCobName", cobName.getText())
    .field("sendDealerID", dealerID.getText())
    .field("sendJobType", jobType.getValue().toString())
    .field("sendStartDate", startDate.getValue().toString())
    .field("sendEndDate", endDate.getValue().toString())
    .field("sendAmount", sentAmount.getText())
    .field("sendCost", cost.getText())
    .field("sendDataDescription", dataDescription.getText())
    .asJson();

您需要将每个字段设置为您要发送的值。