炒股程序的福利
第一步 其实也是最重要的一步
准备好新浪为我们提供好了接口୧(๑•̀⌄•́๑)૭
http://hq.sinajs.cn/list=sz000625,sz300200,sh603156,sh603256,sh603777,sh601186,sz300235,sh603258,sz002517,sz002063,sz002373,sz002475,sz002195,sz002555
list = 后面追加 股票的代码。 返回的就是个股票的 今日最高 今日最低 现价 等数据
第二步 创建好http的get访问
- main 方法 里面 smallConfirMap 是我用来记录 当某股价格低于多少的时候 给出弹出提示
那么bigConfirMap 是指某股价格到大多少的时候给与提示 且这些提示只会有一次
private static DecimalFormat df = new DecimalFormat("0.00%");
private static DecimalFormat priceFormat = new DecimalFormat("00.00");
public static void main(String[] args) throws IOException, InterruptedException {
Map<String, Double> smallConfirmMap = new HashMap<>();
smallConfirmMap.put("养元饮品", 27.00D);
Map<String, Double> bigConfirmMap = new HashMap<>();
bigConfirmMap.put("长安汽车", 7.80D);
String url = "http://hq.sinajs.cn/list=sz000625,sz300200,sh603156,sh603256,sh603777,sh601186,sz300235,sh603258,sz002517,sz002063,sz002373,sz002475,sz002195,sz002555";
doGet(url,smallConfirmMap, bigConfirmMap);
}
2.直接贴访问url代码
public static String doGet(String httpurl, Map<String, Double> smallConfirmMap, Map<String, Double> bigConfirmMap) {
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
String result = null;// 返回结果字符串
int limitValue = 6;
String[] titleArray = {"今开","昨收","现价","今高","今低"};
try {
// 创建远程url连接对象
URL url = new URL(httpurl);
// 通过远程url连接对象打开一个连接,强转成httpURLConnection类
while (true) {
Thread.sleep(3000);
connection = (HttpURLConnection) url.openConnection();
// 设置连接方式:get
connection.setRequestMethod("GET");
// 设置连接主机服务器的超时时间:15000毫秒
connection.setConnectTimeout(15000);
// 设置读取远程返回的数据时间:60000毫秒
connection.setReadTimeout(60000);
// 发送请求
connection.connect();
// 通过connection连接,获取输入流
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
// 封装输入流is,并指定字符集
br = new BufferedReader(new InputStreamReader(is, "GB2312"));
// 存放数据
StringBuilder sbf = new StringBuilder();
String temp = null;
StringBuilder title = new StringBuilder();
title.append(" ");
for (String s : titleArray) {
title.append(s).append(" ");
}
System.out.println(title.toString());
while ((temp = br.readLine()) != null) {
int nsnEnd = temp.indexOf("=");
int start = temp.indexOf('"');
int end = temp.lastIndexOf('"');
String nsan = temp.substring(11, nsnEnd);
String[] contentArray = temp.substring(start + 1, end).split(",");
StringBuilder name = new StringBuilder(String.valueOf(contentArray[0]));
while (name.length() < 4) {
name.append(" ");
}
sbf.append("(").append(nsan).append(")").append(name);
Double nowOpenPrice = 0.00;
Double nowPrice = 0.00;
for (int j = 1; j < limitValue; j++) {
Double value = Double.valueOf(contentArray[j]);
if (j == 2) {//昨收
nowOpenPrice = value;
}
if (j == 3) {//现价
nowPrice = value;
}
sbf.append(" ").append(priceFormat.format(value));
}
double range = (nowPrice / nowOpenPrice - 1);
Iterator<Map.Entry<String, Double>> smallMap = smallConfirmMap.entrySet().iterator();
while (smallMap.hasNext()) {
Map.Entry<String, Double> entry = smallMap.next();
if (name.toString().equals(entry.getKey())) {
if (entry.getValue() > nowPrice) {
JOptionPane.showMessageDialog(null, entry.getKey() + "的价格是" + nowPrice, "标题", JOptionPane.WARNING_MESSAGE);
smallMap.remove();
}
}
}
Iterator<Map.Entry<String, Double>> bigMap = bigConfirmMap.entrySet().iterator();
while (bigMap.hasNext()) {
Map.Entry<String, Double> entry = bigMap.next();
if (name.toString().equals(entry.getKey())) {
if (entry.getValue() < nowPrice) {
JOptionPane.showMessageDialog(null, entry.getKey() + "的价格是" + nowPrice, "标题", JOptionPane.WARNING_MESSAGE);
bigMap.remove();
}
}
}
if (range > 0) {
sbf.append("\033[31;m ").append(df.format(range)).append("\033[0m");
} else if (range == 0) {
sbf.append("\033[31;m ").append(df.format(range)).append("\033[0m");
} else {
sbf.append("\033[32;m ").append(df.format(range)).append("\033[0m");
}
sbf.append("\r\n");
}
result = sbf.toString();
System.out.println(result);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
if (null != br) {
try {
br.close();
} catch (IOException e) {
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
connection.disconnect();// 关闭远程连接
}
return result;
}
3.获得数据主要还是自己要进行切割才能清晰显示
最终效果如图(3秒更新一次):1:昨日收盘价格 2:今日开盘价格 3:现价 4:涨跌幅
提示来的时候:
就这样可以安心的在控制台查看股价的变动了,不会被抓壮丁了哈哈哈哈, 不会被 玩手机而被抓 也不会在电脑上看奇怪的东西而被抓哈哈哈
|´・ω・)ノ6666
xiexie