首页 > 代码库 > JavaFX基础学习之URLConnection
JavaFX基础学习之URLConnection
一个标准的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
JavaFX基础学习之URLConnection
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。