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,测试结果如图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值