2017-09-14 176 views
0

我已經使用Javafx Scene Builder 2.0創建了一個表單我的表單工作並將變量設置爲表單元素值。我還創建了一個接收發布數據並將數據插入數據庫的PHP腳本。發送數據從Javafx發送到HTTP POST請求

我需要一些幫助,通過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 

     }); 
    } 



} 

回答

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

你需要每個字段設置爲您要發送的值。

+0

除了縮短不發送問題中的所有字段外,還有什麼我做錯了嗎? – jrtapsell

+0

擴大到包括所有領域,以防我的答案是問題 – jrtapsell