以下是 Java 调用第三方 BankingCircle 的接口查询汇率接口的示例代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class BankingCircleExchangeRate {
    public static void main(String[] args) {
        try {
            // 设置请求 URL
            String url = 'https://api.bankingcircle.com/v1/exchangerates?from=USD&to=EUR';
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            // 设置请求头
            con.setRequestMethod('GET');
            con.setRequestProperty('Authorization', 'Bearer <your_access_token>');

            // 发送请求
            int responseCode = con.getResponseCode();
            System.out.println('Sending GET request to URL: ' + url);
            System.out.println('Response Code: ' + responseCode);

            // 读取响应
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // 打印响应结果
            System.out.println(response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

其中,需要替换掉<your_access_token>为自己的访问令牌。如果访问成功,会返回如下格式的 JSON 数据:

{
    'from': 'USD',
    'to': 'EUR',
    'rate': 0.835,
    'timestamp': '2021-01-01T00:00:00Z'
}

其中,rate 字段表示汇率。

Java 调用 BankingCircle API 查询汇率接口示例

原文地址: https://www.cveoy.top/t/topic/j56C 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录