对接印度数据源股票API实战指南
一、API服务简介
本教程基于某国际金融数据平台提供的印度股票市场API接口,支持获取实时行情、历史K线、市场新闻等金融数据。接口采用RESTful设计,支持JSON格式返回。
二、快速接入步骤
1. 获取API密钥
访问官网联系客服获取免费测试KEY(示例:MY4b781f618e3f43c4b055f25fa61941ad
)
2. 创建Java项目
使用Maven构建项目,添加依赖:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.12.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
3. 封装HTTP工具类
public class StockApiClient {
private static final String BASE_URL = "https://api.stocktv.top/stock/";
private static final String API_KEY = "MY4b781f618e3f43c4b055f25fa61941ad";
private OkHttpClient client = new OkHttpClient();
private Gson gson = new Gson();
private String buildUrl(String path, Map<String,String> params) {
HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + path).newBuilder();
params.forEach(urlBuilder::addQueryParameter);
urlBuilder.addQueryParameter("key", API_KEY);
return urlBuilder.build().toString();
}
public <T> T executeRequest(String path, Map<String,String> params, Class<T> responseType) throws IOException {
Request request = new Request.Builder()
.url(buildUrl(path, params))
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
return gson.fromJson(response.body().charStream(), responseType);
}
}
}
三、核心接口对接示例
1. 获取印度市场股票列表
// 请求示例
Map<String,String> params = new HashMap<>();
params.put("countryId", "14"); // 印度国家ID
params.put("pageSize", "10");
params.put("page", "1");
StockResponse response = client.executeRequest("stocks", params, StockResponse.class);
// 响应实体
class StockResponse {
int code;
String message;
PageData data;
static class PageData {
List<Stock> records;
int total;
}
static class Stock {
int id; // 股票ID
String symbol; // 股票代码
String name; // 公司名称
double last; // 最新价
double chgPct; // 涨跌幅
// 其他字段...
}
}
2. 查询指定股票K线数据
Map<String,String> params = new HashMap<>();
params.put("pid", "7310"); // 股票ID
params.put("interval", "PT15M"); // 15分钟K线
KlineResponse response = client.executeRequest("kline", params, KlineResponse.class);
// K线数据结构
class KlineItem {
long time; // 时间戳
double open; // 开盘价
double high; // 最高价
double low; // 最低价
double close; // 收盘价
double volume; // 成交量
}
3. 获取实时新闻数据
Map<String,String> params = new HashMap<>();
params.put("pageSize", "5");
params.put("countryId", "14");
NewsResponse response = client.executeRequest("news", params, NewsResponse.class);
class NewsItem {
String title; // 新闻标题
String publishTime; // 发布时间
String description; // 内容摘要
String url; // 原文链接
}
四、WebSocket实时数据订阅
// 建立WebSocket连接
WebSocket ws = new WebSocketClient(
URI.create("wss://ws-api.stocktv.top/connect?key=MY4b781f618e3f43c4b055f25fa61941ad")) {
@Override
public void onMessage(String text) {
// 处理实时行情推送
System.out.println("Received: " + text);
}
};
// 订阅印度NIFTY指数
ws.send("{\"action\":\"subscribe\",\"channel\":\"indices:17940\"}");
五、注意事项
- 免费版API限制:没有限制
- 数据更新频率:股票行情15秒刷新,新闻数据5分钟更新
- 需遵守接口协议,禁止商业用途和二次分发
- 生产环境建议添加请求重试机制和异常处理
以上为完整的接口对接指南,开发者可基于业务需求扩展更多功能。实际调用时请替换为正式API密钥并遵守平台使用条款。