java实时获取汇率

您所在的位置:网站首页 外币兑换器软件 java实时获取汇率

java实时获取汇率

2024-01-15 14:45| 来源: 网络整理| 查看: 265

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 headers = new HashMap(); //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 headers.put("Authorization", "APPCODE " + appcode); Map querys = new HashMap(); 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:

org.apache.httpcomponents httpclient 4.2.1 org.apache.httpcomponents httpcore 4.2.1 commons-lang commons-lang 2.6

然后是HttpUtils.java:HttpUtils

测试方法的配置:

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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3