新浪股票插口获取历史数据
优采云 发布时间: 2020-08-09 10:30这三天做了一个调用新浪股票插口获取实时以及历史*敏*感*词*的应用,因为新浪没有公开关于其插口的官方文档,所以通过各类百度差了好多关于新浪股票插口的使用,不过你们基本都是转载或则直接复制,对于实时数据的获取讲的太详尽,但是缺乏获取历史数据的方式。
关于实时数据的获取你们可以看这篇博客:实时*敏*感*词*插口
经过不懈的努力总算再这篇博文中找到了关于新浪股票历史数据的获取方法腾讯股票插口、和讯网股票插口、新浪股票插口、雪球*敏*感*词*、网易*敏*感*词*
最近二十天左右的每5分钟数据
(参数:股票编号、分钟间隔(5、15、30、60)、均值(5、10、15、20、25)、查询个数点(最大值242))
获取的数据是类似下边的json字段:日期、开盘价、最高价、最低价、收盘价、成交量:
获取的数据会有很多,然后按照自己须要进行解析,我需要的是每晚的收盘价,股市是每个工作日下午3点午盘,所以我只须要找到每晚的下午三点时刻的数据进行过滤即可:
1、新建一个历史数据对象类:
public class HistoryModel {
public String day;
public String close;
public HistoryModel(String day, String close) {
this.day = day;
this.close = close;
}
}
2、新建一个股票多次历史数据类:和上一个区别就是,这里收录的是所有的历史数据:参数包括股票名子、代码、现在的价钱、历史数据:
public class HistoryModels {
public String name;
public String code;
public String now;
public List list;
public HistoryModels(String name, String code, String now, List list) {
this.name = name;
this.code = code;
this.now = now;
this.list = list;
}
}
3、将须要查询的股票的代码带进url里通过HTTP恳求json数据,我这儿用的Volley恳求的:
其中将时间点未15:00:00的数据过滤下来,组合乘List以后在全部形参组合成一个HistoryModels储存股票信息以及股票的所有历史数据。
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(Home.context);
String url1 = "http://money.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_MarketData.getKLineData?symbol=" + Home.myChoiceModelList.get(ii).code + "&scale=60&ma=no&datalen=1023";
// Request a string response from the provided URL.
StringRequest stringRequest1 = new StringRequest(Request.Method.GET, url1,
new Response.Listener() {
@Override
public void onResponse(String response) {
List historyList = Convert(response,new TypeToken() {
}.getType());
List historyList2 = new ArrayList();
if(historyList!=null) {
for (int j = 0; j < historyList.size(); j++) {
if (historyList.get(j).day.split(" ")[1].equals("15:00:00")) {
historyList2.add(historyList.get(j));
}
}
}
HistoryModels model = new HistoryModels(Home.myChoiceModelList.get(ii).name, Home.myChoiceModelList.get(ii).code, Home.myChoiceModelList.get(ii).now, historyList2);
cllList.add(model);
Message msg = new Message();
msg.what = 0x002;
handler.sendMessage(msg);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(stringRequest1);
4、其中对json数据的处理,即从json转化成数据对象的方式如下:
/*
* Json转换泛型
*/
public static T Convert(String jsonString, Type cls) {
T t = null;
try {
if (jsonString != null && !jsonString.equals("")) {
Gson gson = new Gson();
t = gson.fromJson(jsonString, cls);
}
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
关于股票的实时数据这儿没有描述,通过文章开头联接的博客可以了解到,写的太详尽。