JavaFX基础学习之URLConnection

时间:2024-04-05 19:35:23

一个标准的JavaFX文件包含三个部分:主类 、 控制类、 界面设计(XML+CSS)

1,main.java

package application;

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

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.setTitle("the key to the kingdom");
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

2,Conntroller.java

package application;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class Controller implements Initializable{

    @FXML
    private TextArea textarea;
    @FXML
    private TextField textfield;
    @FXML
    private Button button;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        try {
            String corpid = "TEST";
            String corpsecret = "TEST";
            URL url = new URL("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret);
            URLConnection uc = url.openConnection();
            InputStream raw = uc.getInputStream();
            InputStream buffer = new BufferedInputStream(raw);
            Reader r = new InputStreamReader(buffer);
            int c;
            StringBuffer body = new StringBuffer();
            while ((c = r.read()) != -1){

            body.append((char)c);

            }
            String key = body.substring(17, 81);

            String xuehao = textfield.getText();
            URL url2 = new URL("https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token="+key+"&userid="+xuehao);
            URLConnection uc2 = url2.openConnection();
            InputStream in = uc2.getInputStream();
            in = new BufferedInputStream(in);
            Reader r2 = new InputStreamReader(in);
            int i;
            StringBuffer body2 = new StringBuffer();
            while ((i = r2.read()) != -1){
                body2.append((char)i);
            }

            textarea.appendText(body2.toString()+"\n");

        } catch (MalformedURLException ex) {

        } catch (IOException ex) {

        }

    }

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub

    }

}

3,main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="420.0" prefWidth="378.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
    <children>
        <Button fx:id="button" layoutX="264.0" layoutY="20.0" onAction="#handleButtonAction" text="一键获取" />
      <TextField fx:id="textfield" layoutX="97.0" layoutY="20.0" prefHeight="23.0" prefWidth="157.0" />
      <TextArea fx:id="textarea" layoutX="7.0" layoutY="51.0" prefHeight="362.0" prefWidth="364.0" wrapText="true" />
      <Label layoutX="23.0" layoutY="20.0" prefHeight="23.0" prefWidth="91.0" text="请输入学号:" />
    </children>
</AnchorPane>

4,application.css