查询当前已发布的关于股票信息的Web服务接口。 设计一个股票查询的用户界面,允许用户输入待查询的股票代码或股票名称,显示该股票的当前价格,成交量,历史最高价,历史最低价等相关信息。 java实现利用新浪财经api
时间: 2024-04-01 13:34:45 浏览: 95
输入股票代码查询股票信息
好的,以下是查询股票信息的Web服务接口:
- 股票实时行情接口:http://hq.sinajs.cn/list=股票代码(例如:http://hq.sinajs.cn/list=sh601006)
- 股票历史行情接口:http://money.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_MarketData.getKLineData?symbol=股票代码&scale=时间间隔&ma=no&datalen=行数(例如:http://money.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_MarketData.getKLineData?symbol=sh601006&scale=240&ma=no&datalen=1000)
请注意,以上接口仅供参考,使用时需遵守相关规定和条款。
接下来是一个简单的Java实现,实现了用户输入股票代码或名称查询相关信息的功能:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import org.json.JSONObject;
public class StockQuery {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入股票代码或名称:");
String stock = sc.nextLine();
sc.close();
String stockCode = getStockCode(stock);
if (stockCode == null) {
System.out.println("未找到该股票,请检查输入是否正确!");
return;
}
String realtimeData = getRealtimeData(stockCode);
String[] realtimeInfo = parseRealtimeData(realtimeData);
String historyData = getHistoryData(stockCode);
JSONObject historyInfo = parseHistoryData(historyData);
System.out.println("股票代码:" + stockCode);
System.out.println("当前价格:" + realtimeInfo[3]);
System.out.println("成交量:" + realtimeInfo[8]);
System.out.println("历史最高价:" + historyInfo.getJSONArray("high").get(0));
System.out.println("历史最低价:" + historyInfo.getJSONArray("low").get(0));
}
// 根据股票名称或代码获取股票代码
private static String getStockCode(String stock) {
String result = null;
try {
URL url = new URL("http://suggest3.sinajs.cn/suggest/type=&key=" + stock + "&name=suggestdata_1");
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK"));
String line;
while ((line = in.readLine()) != null) {
String[] stockInfo = line.split(",");
if (stockInfo[0].equals("var suggestdata_1={")) {
for (int i = 1; i < stockInfo.length; i++) {
String[] stockDetail = stockInfo[i].split("\"");
if (stockDetail[1].contains("sh") || stockDetail[1].contains("sz")) {
result = stockDetail[1];
break;
}
}
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// 获取股票实时行情数据
private static String getRealtimeData(String stockCode) {
String result = null;
try {
URL url = new URL("http://hq.sinajs.cn/list=" + stockCode);
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK"));
result = in.readLine();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// 解析股票实时行情数据
private static String[] parseRealtimeData(String realtimeData) {
String[] result = null;
if (realtimeData != null) {
String[] stockInfo = realtimeData.split(",");
result = new String[stockInfo.length];
for (int i = 0; i < stockInfo.length; i++) {
result[i] = stockInfo[i].replace("\"", "").replace("=", ":");
}
}
return result;
}
// 获取股票历史行情数据
private static String getHistoryData(String stockCode) {
String result = null;
try {
URL url = new URL("http://money.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_MarketData.getKLineData?symbol=" + stockCode + "&scale=240&ma=no&datalen=1000");
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK"));
StringBuilder sb = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
result = sb.toString();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// 解析股票历史行情数据
private static JSONObject parseHistoryData(String historyData) {
JSONObject result = null;
if (historyData != null) {
result = new JSONObject(historyData);
}
return result;
}
}
```
阅读全文