[英]How to read data from URL and use it in a linechart? Getting JAVAFX errors
I have created an application which reads the price of bitcoin from an online API on IntelliJ using java and javafx. 我创建了一个应用程序,该应用程序使用java和javafx从IntelliJ上的在线API读取比特币的价格。 The compiler does not identify any errors, no errors in the fxml file either, but when I run the application, I get multiple javafx errors. 编译器无法识别任何错误,也不会在fxml文件中识别任何错误,但是当我运行该应用程序时,我遇到了多个javafx错误。 How can I fix it? 我该如何解决? I tried different methods but it seems that something is disconnected in the fxml file. 我尝试了不同的方法,但似乎fxml文件中的某些内容已断开连接。 It also seems that the error is coming from my loadChart() method, because when I remove it, all is working smoothly. 似乎该错误也来自我的loadChart()方法,因为当我删除它时,所有操作都顺利进行。 How could enter code here
I link the yValue to the price from the API without errors? 我如何将yValue链接到API的价格而没有错误enter code here
? Could you please help? 能否请你帮忙?
Thank you. 谢谢。
Main class:
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("Bitcoin Tracker");
primaryStage.setScene(new Scene(root, 600, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller class: 控制器类:
package sample;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.*;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.util.*;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javafx.stage.*;
import javafx.application.Application;
public class Controller {
BTC my_price;
@FXML // fx:id="price_label";
Label price_label;
High highprice;
@FXML // fx:id="high_label";
Label high_label;
Low lowprice;
@FXML // fx:id="low_price"
Label low_label;
VolumeFrom vfrom;
@FXML // fx:id="vfrom_label"
Label vfrom_label;
VolumeTo vto;
@FXML // fx:id="vto_label"
Label vto_label;
@FXML // fx:id="chart";
LineChart<String, Number> line_chart;
ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
public Controller(){
my_price = new BTC();
highprice = new High();
lowprice = new Low();
vfrom = new VolumeFrom();
vto = new VolumeTo();
Controller ctrl = this;
exec.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
ctrl.refreshCurrency();
}
}, 1, 1, TimeUnit.MINUTES);
}
public void refreshCurrency() {
System.out.println("Refreshing...");
CompletableFuture<Double> future = new CompletableFuture<Double>();
future.supplyAsync(() -> {
my_price.getCurrency();
Platform.runLater(() -> {
price_label.setText(new Double(my_price.price).toString());
});
return 1.0;
});
future.supplyAsync(() -> {
highprice.getHigh();
Platform.runLater(() -> {
high_label.setText(new Double(highprice.highprice).toString());
});
return 1.0;
});
future.supplyAsync(() -> {
lowprice.getLow();
Platform.runLater(() -> {
low_label.setText(new Double(lowprice.lowprice).toString());
});
return 1.0;
});
future.supplyAsync(() -> {
vfrom.getVfrom();
Platform.runLater(() -> {
vfrom_label.setText(new Double(vfrom.vfrom).toString());
});
return 1.0;
});
future.supplyAsync(() -> {
vto.getVto();
Platform.runLater(() -> {
vto_label.setText(new Double(vto.vto).toString());
});
return 1.0;
});
System.out.println("Refreshing high...");
}
public void loadChart(){
line_chart.setTitle("BTC");
XYChart.Series series = new XYChart.Series();
double BTCprice = Double.parseDouble(BTC.readFromAPI());
Calendar c = Calendar.getInstance();
Date start = new Date(1521171240000L);
System.out.println(start);
series.getData().add(new XYChart.Data<>(c.getTime().toString(),(BTCprice)*1000));
c.add(Calendar.HOUR_OF_DAY, 1);
line_chart.getData().add(series);
line_chart.getXAxis().setLabel("Time");
line_chart.getXAxis().setLabel("Price");
}
}
BTC class: BTC类别:
package sample;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class BTC {
double price;
private static String url = "https://min-api.cryptocompare.com/data/histominute?aggregate=0&e=CCCAGG&extraParams=CryptoCompare&fsym=BTC&limit=100&tryConversion=false&tsym=USD";
public static String readFromAPI(){
String contents = "";
try {
URL address = new URL(url);
InputStreamReader reader = new
InputStreamReader(address.openStream());
BufferedReader buffer = new BufferedReader(reader);
String line = "";
while ((line = buffer.readLine()) != null) {
if (line.isEmpty()) {
break;
}
contents += line;
}
}catch(Exception e){
e.printStackTrace();
}
return contents;
}
public void getCurrency(){
try {
String from_api = readFromAPI();
Gson gson = new Gson();
JsonObject response = gson.fromJson(from_api, JsonObject.class);
JsonArray data = response.getAsJsonArray("Data");
for(JsonElement item : data ){
JsonObject item_as_hashmap = item.getAsJsonObject();
this.price = item_as_hashmap.get("close").getAsDouble();
}
}catch(Exception exc){
exc.printStackTrace();
}
}
}
FXML file: FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<Pane onMouseClicked="#refreshCurrency" prefHeight="472.0" prefWidth="626.0">
<children>
<Label layoutX="213.0" layoutY="22.0" text="Bitcoin Tracker">
<font>
<Font name="System Bold" size="24.0" />
</font>
</Label>
<Label layoutX="34.0" layoutY="24.0" text="BTC">
<font>
<Font size="22.0" />
</font>
</Label>
<Label fx:id="price_label" layoutX="95.0" layoutY="25.0" onMouseClicked="#refreshCurrency" text="Label">
<font>
<Font size="21.0" />
</font>
</Label>
<Label layoutX="39.0" layoutY="84.0" text="Volume from:" />
<Label layoutX="39.0" layoutY="114.0" text="Volume to:" />
<Label fx:id="vfrom_label" layoutX="119.0" layoutY="84.0" onMouseClicked="#refreshCurrency" text="Label" />
<Label fx:id="vto_label" layoutX="119.0" layoutY="114.0" onMouseClicked="#refreshCurrency" text="Label" />
<Label layoutX="479.0" layoutY="84.0" text="High" />
<Label layoutX="479.0" layoutY="114.0" text="Low" />
<Label fx:id="high_label" layoutX="526.0" layoutY="84.0" text="Label" />
<Label fx:id="low_label" layoutX="526.0" layoutY="114.0" onMouseClicked="#refreshCurrency" text="Label" />
<LineChart fx:id="line_chart" layoutX="40.0" layoutY="171.0" onMouseClicked="#loadChart" prefHeight="273.0" prefWidth="582.0">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis prefHeight="232.0" prefWidth="25.0" side="LEFT" />
</yAxis>
</LineChart>
</children>
</Pane>
</children>
</GridPane>
Once I run the main class, and the pane opens, I click once, and then get the following errors: 运行主类并打开窗格后,单击一次,然后出现以下错误:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3589)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.access$8300(Scene.java:3517)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3885)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Scene.java:3604)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1874)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2613)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
... 31 more
要使用HTTPS调用api,请使用HTTP客户端(例如apache http客户端https://hc.apache.org/)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.