最近在做项目,涉及到实时汇率的获取,查了很多方法。下面写下自己获取实时汇率的心得,希望帮到有需要的人。
我使用的是聚合数据,免费的货币汇率API https://www.juhe.cn/docs/api/id/23 。
首先要注册用户,进行实名认证后,申请到免费的APPKEY ,就可以使用货币汇率API了。
人民币碑价:
接口地址:http://web.juhe.cn:8080/finance/exchange/rmbquot
返回格式:json
请求方式:get
请求示例:http://web.juhe.cn:8080/finance/exchange/rmbquot?key=您申请的APPKEY
接口备注:100外币兑人民币,更新时间2分钟,此汇率仅供参考,以中国银行各分行实际交易汇率为准,不构成投资建议。投资者据此买卖,风险自担
名称 | 必填 | 类型 | 说明 | |
---|---|---|---|---|
key | 是 | String | APP Key | |
type | 否 | int | 两种格式(0或者1,默认为0) | |
bank | 否 | int | (0:工商银行,1:招商银行,2:建设银行,3:中国银行,4:交通银行,5:农业银行,默认为:中国银行)
|
自己写的Java例子:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;
//注意:使用的是com.alibaba.fastjson.JSONArray ,不是net.sf.json.JSONObjec
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class CurrencyRate {
public static final String DEF_CHATSET = "UTF-8";
public static final int DEF_CONN_TIMEOUT = 30000;
public static final int DEF_READ_TIMEOUT = 30000;
public static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
//配置您申请的APPKEY
public static final String APPKEY ="**********************";
static Double[] eRate = new Double[10];
public static void main(String[] args) {
String result1 =null;
String url ="http://web.juhe.cn:8080/finance/exchange/rmbquot";//请求接口地址
Map params = new HashMap();//请求参数
params.put("key",APPKEY);//APP Key
params.put("type","");//两种格式(0或者1,默认为0)
try {
result1 =net(url, params, "GET");
//将字符串转化成JSON对象
JSONObject object = JSONObject.parseObject(result1);
//转化成JSON数组
JSONArray resultList = object.getJSONArray("result");
//取出JSON数组中的值
for (int i=0; idata) {
StringBuilder sb = new StringBuilder();
for (Map.Entry i : data.entrySet()) {
try {
sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
输出结果:
JSON返回示例:
{
"resultcode":"200",
"reason":"SUCCESSED!",
"result":[
{
"data1":{
"name":"英镑", /*货币名称*/
"fBuyPri":"1001.430", /*现汇买入价*/
"mBuyPri":"970.510", /*现钞买入价*/
"fSellPri":"1009.480", /*现汇卖出价*/
"mSellPri":"1009.480", /*现钞卖出价*/
"bankConversionPri":"1014.870", /*银行折算价/中间价*/
"date":"2012-12-13", /*发布日期*/
"time":"16:25:49" /*发布时间*/
},
"data2":{
"name":"港币",
"fBuyPri":"80.210",
"mBuyPri":"79.570",
"fSellPri":"80.520",
"mSellPri":"80.520",
"bankConversionPri":"81.140",
"date":"2012-12-13",
"time":"16:25:49"
},
"data3":{
"name":"美元",
"fBuyPri":"621.650",
"mBuyPri":"616.670",
"fSellPri":"624.150",
"mSellPri":"624.150",
"bankConversionPri":"628.870",
"date":"2012-12-13",
"time":"16:25:49"
},
"data4":{
"name":"瑞士法郎",
"fBuyPri":"670.310",
"mBuyPri":"649.620",
"fSellPri":"675.700",
"mSellPri":"675.700",
"bankConversionPri":"674.350",
"date":"2012-12-13",
"time":"16:25:49"
},
"data5":{
"name":"新加坡元",
"fBuyPri":"508.300",
"mBuyPri":"492.610",
"fSellPri":"512.390",
"mSellPri":"512.390",
"bankConversionPri":"512.000",
"date":"2012-12-13",
"time":"16:25:49"
},
"data6":{
"name":"瑞典克朗",
"fBuyPri":"93.340",
"mBuyPri":"90.450",
"fSellPri":"94.090",
"mSellPri":"94.090",
"bankConversionPri":"93.770",
"date":"2012-12-13",
"time":"16:25:49"
},
"data7":{
"name":"丹麦克朗",
"fBuyPri":"108.920",
"mBuyPri":"105.550",
"fSellPri":"109.790",
"mSellPri":"109.790",
"bankConversionPri":"109.430",
"date":"2012-12-13",
"time":"16:25:49"
},
"data8":{
"name":"挪威克朗",
"fBuyPri":"110.490",
"mBuyPri":"107.080",
"fSellPri":"111.380",
"mSellPri":"111.380",
"bankConversionPri":"111.060",
"date":"2012-12-13",
"time":"16:25:49"
},
"data9":{
"name":"日元",
"fBuyPri":"7.431",
"mBuyPri":"7.201",
"fSellPri":"7.483",
"mSellPri":"7.483",
"bankConversionPri":"7.531",
"date":"2012-12-13",
"time":"16:25:49"
},
"data10":{
"name":"加拿大元",
"fBuyPri":"630.950",
"mBuyPri":"611.470",
"fSellPri":"636.010",
"mSellPri":"636.010",
"bankConversionPri":"638.800",
"date":"2012-12-13",
"time":"16:25:49"
},
"data11":{
"name":"澳大利亚元",
"fBuyPri":"654.900",
"mBuyPri":"634.680",
"fSellPri":"660.160",
"mSellPri":"660.160",
"bankConversionPri":"663.300",
"date":"2012-12-13",
"time":"16:25:49"
},
"data12":{
"name":"欧元",
"fBuyPri":"812.520",
"mBuyPri":"787.430",
"fSellPri":"819.040",
"mSellPri":"819.040",
"bankConversionPri":"821.960",
"date":"2012-12-13",
"time":"16:25:49"
},
"data13":{
"name":"澳门元",
"fBuyPri":"77.910",
"mBuyPri":"75.300",
"fSellPri":"78.210",
"mSellPri":"80.720",
"bankConversionPri":"78.320",
"date":"2012-12-13",
"time":"16:25:49"
},
"data14":{
"name":"菲律宾比索",
"fBuyPri":"15.100",
"mBuyPri":"14.640",
"fSellPri":"15.220",
"mSellPri":"15.690",
"bankConversionPri":"15.220",
"date":"2012-12-13",
"time":"16:25:49"
},
"data15":{
"name":"泰国铢",
"fBuyPri":"20.240",
"mBuyPri":"19.620",
"fSellPri":"20.400",
"mSellPri":"21.030",
"bankConversionPri":"20.390",
"date":"2012-12-13",
"time":"16:25:49"
},
"data16":{
"name":"新西兰元",
"fBuyPri":"524.400",
"mBuyPri":"0.000",
"fSellPri":"528.610",
"mSellPri":"0.000",
"bankConversionPri":"526.770",
"date":"2012-12-13",
"time":"16:25:49"
},
"data17":{
"name":"韩国元",
"fBuyPri":"0.000",
"mBuyPri":"0.560",
"fSellPri":"0.000",
"mSellPri":"0.608",
"bankConversionPri":"0.586",
"date":"2012-12-13",
"time":"16:25:49"
}
}
]
}