使用汇率查询API帮你实时查询汇率,促进货币交流

502 阅读2分钟

汇率查询API以较低的成本获取较高的收益。提供实时汇率换算,外汇报价,货币汇率以及币种之间的汇率转换。广泛用于国际结算、银行汇率查询应用、开展跨国贸易、投资等参考场景。数据来源于公开的信息发布平台。

接口介绍

汇率查询API提供以下三个子接口:

1. 实时汇率查询换算

用于获取不同货币之间的实时汇率,并且可以根据输入金额进行换算。

2. 货币列表

获取系统支持查询汇率的所有货币信息列表,方便开发者了解可操作的货币种类。

3. 单货币列表

查询指定单一货币的详细信息,包括货币代码、名称、汇率等。

代码示例

1.请求参数说明

名称必填类型说明
keystring个人中心查看
fromstring转换汇率前的货币代码
tostring转换汇率成的货币代码
moneystring换算金额

2. 返回参数说明

参数名类型说明
codeint状态码,1表示成功
msgstring消息
fromstring源货币代码
from_namestring源货币名称
tostring目标货币代码
to_namestring目标货币名称
exchangefloat汇率
moneyfloat换算后的金额
updatetimestring汇率更新时间

3. JSON返回示例

{
    "code": 1,
    "msg": "操作成功",
    "data": {
        "from": "CNY",
        "from_name": "人民币",
        "to": "EUR",
        "to_name": "欧元",
        "exchange": "0.120490",
        "money": "0.120490",
        "updatetime": "2025-04-17 15:16:15"
    }
}

4.Java代码示例

接口地址为:https://www.tanshuapi.com/market/detail-84 
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.json.JSONObject;

public class ExchangeRateAPIExample {
    public static void main(String[] args) {
        String apiUrl = "https://api.example.com/exchange/v1/rate";
        String apiKey = "your_api_key_here";
        String fromCurrency = "CNY";
        String toCurrency = "EUR";
        double amount = 1.0;

        try {
            String urlStr = apiUrl + "?key=" + apiKey + "&from=" + fromCurrency + "&to=" + toCurrency + "&amount=" + amount;
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");

            int responseCode = conn.getResponseCode();
            System.out.println("响应码: " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                System.out.println("响应结果: " + response.toString());

                JSONObject jsonResponse = new JSONObject(response.toString());
                int code = jsonResponse.getInt("code");
                String msg = jsonResponse.getString("msg");
                JSONObject data = jsonResponse.getJSONObject("data");

                System.out.println("状态码: " + code);
                System.out.println("消息: " + msg);
                System.out.println("源货币代码: " + data.getString("from"));
                System.out.println("源货币名称: " + data.getString("from_name"));
                System.out.println("目标货币代码: " + data.getString("to"));
                System.out.println("目标货币名称: " + data.getString("to_name"));
                System.out.println("汇率: " + data.getString("exchange"));
                System.out.println("换算金额: " + data.getString("money"));
                System.out.println("更新时间: " + data.getString("updatetime"));
            } else {
                System.out.println("请求失败");
            }

            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}