java实时获取汇率

1.分享三个觉得挺不错的汇率api:

1) 每小时免费50次查询配额:NOWapi

2) 0.1元2000次/年:阿里云 汇率api

3) 每天免费100次查询配额(需要实名认证):聚合科技

如果只是针对很少外币获取汇率的话,个人推荐去阿里云购买,毕竟1元不到就可以查询非常多次了。

2.以下的总结以通过阿里云的汇率api获取实时汇率为准。

3.首先,这个是 汇率Api使用文档

4.目前官网通过Java来调用api的测试方法如下:

import com.bastriver.common.util.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

public class test {

    public static void main(String[] args) {
        String host = "https://ali-waihui.showapi.com";
        String path = "/waihui-transform";
        String method = "GET";
        String appcode = "***********";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("fromCode", "CNY");
        querys.put("money", "100");
        querys.put("toCode", "JPY");

// https://market.aliyun.com/products/57000002/cmapi010841.html?spm=5176.2020520132.101.2.32087218XQPbLW#sku=yuncode484100003
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            System.out.println(response.toString());
            //获取response的body
            System.out.println(EntityUtils.toString(response.getEntity()));
            /*{
                "showapi_res_error": "",
                    "showapi_res_id": "f72eb3ff9c494d4e90343ac4c36dbf1b",
                    "showapi_res_code": 0,
                    "showapi_res_body": {"ret_code":0,"money":"1585.0875"} //转换后的金额,单位元
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

5.因为使用到阿里的工具,所以先添加pom:

		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.2.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.2.1</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.6</version>
		</dependency>

然后是HttpUtils.java:HttpUtils

测试方法的配置:

比如,例子中的是通过人民币的100元可以兑换日币1585.0875,测试结果如图:

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下的Java代码获取实时汇率信息: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; public class CurrencyConverter { public static void main(String[] args) { String fromCurrency = "USD"; String toCurrency = "EUR"; double amount = 100; double exchangeRate = getExchangeRate(fromCurrency, toCurrency); double convertedAmount = exchangeRate * amount; System.out.println(amount + " " + fromCurrency + " = " + convertedAmount + " " + toCurrency); } public static double getExchangeRate(String fromCurrency, String toCurrency) { String url = "https://api.exchangerate-api.com/v4/latest/" + fromCurrency; try { URL urlObj = new URL(url); HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection(); connection.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); String jsonResponse = response.toString(); double exchangeRate = Double.parseDouble(jsonResponse.split("\"" + toCurrency + "\":")[1].split(",")[0]); return exchangeRate; } catch (Exception e) { e.printStackTrace(); return 0; } } } ``` 上述的代码使用了 https://exchangerate-api.com 提供的 API 来获取实时汇率信息。你需要将上述的代码中的 `fromCurrency` 和 `toCurrency` 更改为你所需的货币代码,并将 `amount` 更改为你所需的金额。你也可以使用其他的实时汇率 API 来获取汇率信息,只需要将上述代码中的 `url` 更改为对应的 API 地址即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值