我正试图为我的java外汇程序获取实时汇率。我看到,这可以使用API从互联网和导入网站URL的java程序,以获得实时汇率。但是,我在使用JSON时遇到了问题,并且还会遇到一些错误,这些错误阻碍了我运行这个程序。我不知道要导入什么来修复错误。我是新来的,我不知道这是否应该是困难的,还是我在这里做错了什么。提前谢谢你。
`
package currencyConverterGUI;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat; // import for decimal place limitation
import java.net.URL;
import org.json.JSONObject;
import java.io.*;
import java.net.URLConnection;
public class currencyGUI extends JFrame //inherit from JFrame
{
private static final DecimalFormat df = new DecimalFormat("0.00000"); // use DecimalFormat to round double numbers to 5 decimal places
private JButton btnConvert; // generated by GUI designer
private JPanel JPanelMain; // generated by GUI designer
private JTextField textAmount; // generated by GUI designer
private JComboBox textFrom; // generated by GUI designer
private JComboBox textTo; // generated by GUI designer
private JLabel result; // generated by GUI designer
public currencyGUI() {
btnConvert.addActionListener(new ActionListener() { // button reacts to user click; generated by GUI designer
@Override
public void actionPerformed(ActionEvent e)
{
double total;
double amount = Double.parseDouble(textAmount.getText()); // check if input amount is a number and read the input if it is a number
int index = textTo.getSelectedIndex(); //get index of selected currency from the first combo box
if(textFrom.getSelectedItem() == "USD") // if USD is selected in the first combo box, then switch for each currency
{
switch (index) {
case 0:
total = amount * 1;
result.setText(df.format(total) + " USD");
break;
case 1:
total = amount * 0.86;
result.setText(df.format(total) + " EUR");
break;
case 2:
total = amount * 1.88;
result.setText(df.format(total) + " BGN");
break;
case 3:
total = amount * 0.000060;
result.setText(df.format(total) + " BTC");
break;
case 4:
total = amount * 2.98;
result.setText(df.format(total) + " ADA");
break;
}
}
if(textFrom.getSelectedItem() == "EUR") // if EUR is selected in the first combo box, then switch for each currency
{
switch (index) {
case 0:
total = amount * 1.04;
result.setText(df.format(total) + " USD");
break;
case 1:
total = amount * 0.1;
result.setText(df.format(total) + " EUR");
break;
case 2:
total = amount * 1.95;
result.setText(df.format(total) + " BGN");
break;
case 3:
total = amount * 0.000063;
result.setText(df.format(total) + " BTC");
break;
case 4:
total = amount * 3.18;
result.setText(df.format(total) + " ADA");
break;
}
}
if(textFrom.getSelectedItem() == "BGN") // if BGN is selected in the first combo box, then switch for each currency
{
switch (index) {
case 0:
total = amount * 0.53;
result.setText(df.format(total) + " USD");
break;
case 1:
total = amount * 0.51;
result.setText(df.format(total) + " EUR");
break;
case 2:
total = amount * 1;
result.setText(df.format(total) + " BGN");
break;
case 3:
total = amount * 0.000032;
result.setText(df.format(total) + " BTC");
break;
case 4:
total = amount * 1.63;
result.setText(df.format(total) + " ADA");
break;
}
}
if(textFrom.getSelectedItem() == "BTC") // if BTC is selected in the first combo box, then switch for each currency
{
switch (index) {
case 0:
total = amount * 16446.8;
result.setText(df.format(total) + " USD");
break;
case 1:
total = amount * 15851.4;
result.setText(df.format(total) + " EUR");
break;
case 2:
total = amount * 31043.1;
result.setText(df.format(total) + " BGN");
break;
case 3:
total = amount * 1;
result.setText(df.format(total) + " BTC");
break;
case 4:
total = amount * 50467.4;
result.setText(df.format(total) + " ADA");
break;
}
}
if(textFrom.getSelectedItem() == "ADA") // if ADA is selected in the first combo box, then switch for each currency
{
switch (index) {
case 0:
total = amount * 0.33;
result.setText(df.format(total) + " USD");
break;
case 1:
total = amount * 0.32;
result.setText(df.format(total) + " EUR");
break;
case 2:
total = amount * 0.62;
result.setText(df.format(total) + " BGN");
break;
case 3:
total = amount * 0.000020;
result.setText(df.format(total) + " BTC");
break;
case 4:
total = amount * 1;
result.setText(df.format(total) + " ADA");
break;
}
}
}
});
}
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("Currency Converter");
frame.setContentPane(new currencyGUI().JPanelMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true); // make pane visible
URL url = new URL("https://api.exchangeratesapi.io/latest?symbols=USD,GBP");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String jsonText = readAll(in);
JSONObject yourData = new JSONObject(jsonText);
}
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
}`
我试过进口
进口org.json.JSONObject;
和
进口java.net.URLConnection;
but this doesn't fix the error.发布于 2022-11-16 19:12:52
所以你的代码中有很多事情要做。我建议你重构你的程序,它有很多重复。
关于所需图书馆:
导入JSONObject可能不起作用,因为您还没有在依赖项中列出它。
有关maven签出:https://mvnrepository.com/artifact/org.json/json
如果您是,而不是使用依赖关系管理工具的,请从这里下载jar:https://mvnrepository.com/artifact/org.json/json
import java.net.URLConnection;也没有用
我用了import java.net.HttpURLConnection; import java.net.URL;,它起了作用。
发布于 2022-11-16 18:59:40
不同的服务器产生稍微不同的文件。您需要构建应用程序来解析JSON文件,这符合模板的布局方式。你只需要做一次。
https://stackoverflow.com/questions/74465664
复制相似问题