在程序启动期间使用Java访问Web信息

在程序启动期间使用Java访问Web信息,java,python-3.x,Java,Python 3.x,我对Java语言是完全陌生的,我的第一个项目编写了50行这么长的代码,允许您输入一种货币,一种以上述货币表示的金额,以及以其他货币表示的相对金额(包括联盟信用),我遇到了一些麻烦。代码执行得非常完美,唯一的问题是比特币汇率和其他货币一样波动很快。有没有什么方法可以让程序在每次启动时从web上获取信息,以获取相关的汇率? 优选地,作为可在主方法中运行的方法。 另外,如果有人能将此应用于python,那将非常感谢。您需要使用HttpURLConnection访问您最喜欢的exchange url,然

我对Java语言是完全陌生的,我的第一个项目编写了50行这么长的代码,允许您输入一种货币,一种以上述货币表示的金额,以及以其他货币表示的相对金额(包括联盟信用),我遇到了一些麻烦。代码执行得非常完美,唯一的问题是比特币汇率和其他货币一样波动很快。有没有什么方法可以让程序在每次启动时从web上获取信息,以获取相关的汇率? 优选地,作为可在主方法中运行的方法。
另外,如果有人能将此应用于python,那将非常感谢。

您需要使用
HttpURLConnection
访问您最喜欢的exchange url,然后获取html内容,解析它,并获取比特币汇率。
检查此示例,然后可以根据业务逻辑查找contents变量中的值:

package com.jalalkiswani.stackoverflow.answers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class UrlTest {
    public static String NEW_LINE = System.getProperty("line.separator");

    public static void main(String[] args) throws MalformedURLException, IOException {
        URL url = new URL("http://www.google.com");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.connect();
        InputStream inputStream = con.getInputStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuffer contents = new StringBuffer();
        String line;
        while ((line = reader.readLine()) != null) {
            contents.append(line);
            contents.append(NEW_LINE);
        }
        inputStream.close();
        System.out.println(contents);

    }
}
以下示例将从中提取比特币兑换美元的汇率:

重要提示:您必须检查这样做是否合法

编辑 第二个示例的屏幕截图:

是,前提是有一个可导入的软件包,该软件包包含浏览internet的类,并提供您需要的信息。我尝试了列出的代码,但它没有打印出当前汇率。第二个示例将在控制台上为您打印汇率。但是,您应该将此技术用于您最喜欢的exchange站点。我已经更新了我的答案,包括截图。我是一个新的程序员。你能添加注释让我更容易理解代码吗?当然,我刚刚用详细的注释更新了第二个示例。
package a.b.c;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class UrlTest {
    public static String NEW_LINE = System.getProperty("line.seprator");

    public static void main(String[] args) throws MalformedURLException, IOException {
        //URL of your service profile
        URL url = new URL("https://www.coinbase.com/charts");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        //Connect to the website
        con.connect();
        //Open input stream to be able to read data
        InputStream inputStream = con.getInputStream();
        //wrap inputstream in buffer for better performance
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuffer contents = new StringBuffer();
        String line;
        //start reading the webpage response line by line
        while ((line = reader.readLine()) != null) {
            contents.append(line);
            contents.append(NEW_LINE);
        }
        inputStream.close();
        //print the webpage response for debugging, you may remove this line
        System.out.println(contents);

        //this is the element found just before the value in the page , i got it using inspect element in chrome
        String key = "1 BTC = $";
        //get the index of this above key that will return in the response
        int indexOf = contents.indexOf(key);
         //check if found
        if (indexOf != -1) {
            indexOf+=key.length();
            //get the exhange value based on this page response format
            String substring = contents.substring(indexOf, indexOf + 6);
            //print the exchange rate , of course you will need to parse it into double value
            System.out.println(substring);
        } else {
            System.out.println("Not found");
        }

    }

}