Java免费获取实时汇率API
最近在做项⽬,涉及到实时汇率的获取,查了很多⽅法。下⾯写下⾃⼰获取实时汇率的⼼得,希望帮到有需要的⼈。
⾸先要注册⽤户,进⾏实名认证后,申请到免费的APPKEY ,就可以使⽤货币汇率API了。
⼈民币碑价:
返回格式:json
请求⽅式:get
接⼝备注:100外币兑⼈民币,更新时间2分钟,此汇率仅供参考,以中国银⾏各分⾏实际交易汇率为准,不构成投资建议。投资者据此买卖,风险⾃担
名称必填类型说明
key是String APP Key
type否int两种格式(0或者1,默认为0)
(0:⼯商银⾏,1:招商银⾏,2:建设银⾏,3:中国银⾏,4:交通银⾏,5:农业银⾏,默认为:中国银⾏) bank否int
⾃⼰写的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.HttpURLConnection;
import java.URL;
import java.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;
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 = JSONArray("result");
/
/取出JSON数组中的值
for (int i=0; i<resultList.size();i++){
// mBuyPri 是现钞买⼊价
// USD美元
JSONObject json = (JSONObject) (i);
String USD = String("data1");
System.out.println(USD);
JSONObject rJson1 = (JSONObject) ("data1");                    eRate[1] = Double("mBuyPri");
System.out.println(eRate[1]);
//EUP欧元
String EUP = String("data2");
System.out.println(EUP);
JSONObject rJson2 = (JSONObject) ("data2");                    eRate[2] = Double("mBuyPri");
System.out.println(eRate[2]);
// HKD港币
String HKD = String("data3");
System.out.println(HKD);
JSONObject rJson3 = (JSONObject) ("data3");                    eRate[3] = Double("mBuyPri");
System.out.println(eRate[3]);
//JPY⽇元
String JPY = String("data4");
System.out.println(JPY);
JSONObject rJson4 = (JSONObject) ("data4");                    eRate[4] = Double("mBuyPri");
System.out.println(eRate[4]);
// GBP英镑
String GBP = String("data5");
System.out.println(GBP);
JSONObject rJson5 = (JSONObject) ("data5");                    eRate[5] = Double("mBuyPri");
System.out.println(eRate[5]);
// THB泰国铢
String THB = String("data8");
System.out.println(THB);
JSONObject rJson6 = (JSONObject) ("data8");                    eRate[6] = Double("mBuyPri");
System.out.println(eRate[6]);
// MOP澳门元
String MOP = String("data18");
System.out.println(MOP);
JSONObject rJson7 = (JSONObject) ("data18");                    eRate[7] = Double("mBuyPri");
System.out.println(eRate[7]);
//KRW韩国元
System.out.println(KRW);
JSONObject rJson8 = (JSONObject) ("data21");
eRate[8] = Double("mBuyPri");
System.out.println(eRate[8]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param strUrl 请求地址
* @param params 请求参数
* @param method 请求⽅法
* @return  ⽹络请求字符串
* @throws Exception
*/
public static String net(String strUrl, Map params,String method) throws Exception {
HttpURLConnection conn = null;
BufferedReader reader = null;
String rs = null;
try {
StringBuffer sb = new StringBuffer();
if(method==null || method.equals("GET")){
strUrl = strUrl+"?"+urlencode(params);
}
URL url = new URL(strUrl);
conn = (HttpURLConnection) url.openConnection();
if(method==null || method.equals("GET")){
conn.setRequestMethod("GET");
}else{
conn.setRequestMethod("POST");
conn.setDoOutput(true);
}
conn.setRequestProperty("User-agent", userAgent);
conn.setUseCaches(false);
conn.setConnectTimeout(DEF_CONN_TIMEOUT);
conn.setReadTimeout(DEF_READ_TIMEOUT);
conn.setInstanceFollowRedirects(false);
if (params!= null && method.equals("POST")) {java在哪里可以免费自学
try (DataOutputStream out = new OutputStream())) {                        out.writeBytes(urlencode(params));
}
}
InputStream is = InputStream();
reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
String strRead = null;
while ((strRead = adLine()) != null) {
sb.append(strRead);
}
rs = sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
conn.disconnect();
}
}
return rs;
}
//将map型转为请求参数型
public static String urlencode(Map<String,Object>data) {
StringBuilder sb = new StringBuilder();
for (Map.Entry i : Set()) {
try {
sb.Key()).append("=").Value()+"","UTF-8")).append("&");                } catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
String();
}
}
输出结果:
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":"港币",
"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",
},
"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":"泰国铢",

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。