【股票】java+js獲取股票實時資料
呼叫sina專門的js伺服器來解析資料,這種方式我使用了有很長一段時間,速度還是相當不錯的,有時候比專門的行情軟體的實時資料還快,下面是獲取資料的部分程式碼:
String url = "http://hq.sinajs.cn/list=sh600151,sz000830,s_sh000001,s_sz399001,s_sz399106"; try { URL u = new URL(url); byte[] b = new byte[256]; InputStream in = null; ByteArrayOutputStream bo = new ByteArrayOutputStream(); while (true) { try { in = u.openStream(); int i; while ((i = in.read(b)) != -1) { bo.write(b, 0, i); } String result = bo.toString(); String[] stocks = result.split(";"); for (String stock : stocks) { String[] datas = stock.split(","); //根據對照自己對應資料 } bo.reset(); } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (in != null) { in.close(); } } } } catch (Exception ex) { System.out.println(ex.getMessage()); }
list後面的引數為相關的股票程式碼,可以同時附加多個股票程式碼,其中上證前面加"sh",深證前面加"sz", 如果只需獲取股票交易的基本資料則在之前加"s_",如下面的 s_sh000001,s_sz399001,s_sz399106分別為:上證指數,深證成指 ,深證綜指的基本指標資料 獲取到的資料格式如下: var hq_str_sh600151="航天機電,14.49,14.57,14.37,14.86,14.10,14.37,14.38,13146411,190836199,1900,14.37,10601,14.36,16100,14.35,1700,14.34,6900,14.33,49200,14.38,60400,14.39,25373,14.40,4500,14.41,10650,14.42,2008-03-07,15:05:32"; var hq_str_sz000830="魯西化工,8.82,8.91,8.53,8.88,8.38,8.52,8.53,24754397,213611631.24,118400,8.52,38400,8.51,368623,8.50,19000,8.49,69300,8.48,170948,8.53,57160,8.55,44729,8.56,27300,8.57,32700,8.58,2008-03-07,15:20:51"; var hq_str_sh000001="上證指數,4315.808,4360.986,4300.515,4341.295,4265.829,0,0,56747402,92203521881,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2008-03-07,15:05:32"; var hq_str_s_sz399001="深證成指,15560.853,-135.272,-0.86,3500683,963757"; var hq_str_s_sz399106="深證綜指,1369.839,-18.264,-1.32,4279649095,4557752";
獲取資料後,自己根據行情軟體來對應資料,然後使用swing做個介面,呵呵,這樣就大功告成了,個人使用了2個多月了,一般情況下非常好用的。