这两天做了一个调用新浪股票接口获取实时以及历史股票数据的应用,因为新浪没有公开关于其接口的官方文档,所以通过各种百度差了很多关于新浪股票接口的使用,不过大家基本都是转载或者直接复制,对于实时数据的获取讲的很详细,但是缺少获取历史数据的方法。 关于实时数据的获取大家可以看这篇博客:实时股票数据接口 经过不懈的努力终于再这篇博文中找到了关于新浪股票历史数据的获取方式腾讯股票接口、和讯网股票接口、新浪股票接口、雪球股票数据、网易股票数据 最近二十天左右的每5分钟数据 http://money.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_MarketData.getKLineData?symbol=sz000001&scale=5&ma=5&datalen=1023 (参数:股票编号、分钟间隔(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<HistoryModel> list; public HistoryModels(String name, String code, String now, List<HistoryModel> 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<String>() { @Override public void onResponse(String response) { List<HistoryModel> historyList = Convert(response,new TypeToken<List<HistoryModel>>() { }.getType()); List<HistoryModel> 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> 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;} 关于股票的实时数据这里没有描述,通过文章开头连接的博客可以了解到,写的很详细。
|
|