zoukankan      html  css  js  c++  java
  • andoid下的股票行情开发

    1.信息类

      1 public class SinaStockInfo {
      2 
      3     /**
      4      * Sina股票数据接口 以大秦铁路(股票代码:601006)为例,如果要获取它的最新行情,只需访问新浪的股票数据
      5      * 接口:http://hq.sinajs.cn/list=sh601006这个url会返回一串文本,例如:
      6      * 
      7      * var hq_str_sh601006="大秦铁路, 27.55, 27.25, 26.91, 27.55, 26.20, 26.91,
      8      * 26.92, 22114263, 589824680, 4695, 26.91, 57590, 26.90, 14700, 26.89,
      9      * 14300, 26.88, 15100, 26.87, 3100, 26.92, 8900, 26.93, 14230, 26.94,
     10      * 25150, 26.95, 15220, 26.96, 2008-01-11, 15:05:32";
     11      * 
     12      * 这个字符串由许多数据拼接在一起,不同含义的数据用逗号隔开了,按照程序员的思路,顺序号从0开始。 0:”大秦铁路”,股票名字;
     13      * 1:”27.55″,今日开盘价; 2:”27.25″,昨日收盘价; 3:”26.91″,当前价格; 4:”27.55″,今日最高价;
     14      * 5:”26.20″,今日最低价; 6:”26.91″,竞买价,即“买一”报价; 7:”26.92″,竞卖价,即“卖一”报价;
     15      * 8:”22114263″,成交的股票数,由于股票交易以一百股为基本单位,所以在使用时,通常把该值除以一百;
     16      * 9:”589824680″,成交金额,单位为“元”,为了一目了然,通常以“万元”为成交金额的单位,所以通常把该值除以一万;
     17      * 10:”4695″,“买一”申请4695股,即47手; 11:”26.91″,“买一”报价; 12:”57590″,“买二”
     18      * 13:”26.90″,“买二” 14:”14700″,“买三” 15:”26.89″,“买三” 16:”14300″,“买四”
     19      * 17:”26.88″,“买四” 18:”15100″,“买五” 19:”26.87″,“买五”
     20      * 20:”3100″,“卖一”申报3100股,即31手; 21:”26.92″,“卖一”报价 (22, 23), (24, 25),
     21      * (26,27), (28, 29)分别为“卖二”至“卖四的情况” 30:”2008-01-11″,日期; 31:”15:05:32″,时间;
     22      */
     23 
     24     // 股票名字
     25     public String mName;
     26     // 今日开盘价
     27     public float mTodayPrice;
     28     // 昨日收盘价
     29     public float mYestodayPrice;
     30     // 当前价
     31     public float mNowPrice;
     32     // 今日最高价
     33     public float mHighestPrice;
     34     // 今日最低价
     35     public float mLowestPrice;
     36     // 买一价
     37     public float mBuy1Price;
     38     // 卖一价
     39     public float mSell1Price;
     40     // 成交股票数,单位“股”。100股为1手。
     41     public long mTradeCount;
     42     // 成交额,单位“元”。一般需要转换成“万元”。
     43     public long mTradeMoney;
     44     // 买单情况
     45     public BuyOrSellInfo[] mBuy;
     46     // 卖单情况
     47     public BuyOrSellInfo[] mSell;
     48     // 日期
     49     public String mDate;
     50     // 时间
     51     public String mTime;
     52 
     53     // 涨跌 = 现价 - 昨收 ;
     54     // 涨幅 = 涨跌 / 昨收 ;
     55     // 振幅 = (今日最高价 - 今日最低价)/昨收
     56 
     57     private SinaStockInfo(String mName, float mTodayPrice,
     58             float mYestodayPrice, float mNowPrice, float mHighestPrice,
     59             float mLowestPrice, float mBuy1Price, float mSell1Price,
     60             long mTradeCount, long mTradeMoney, BuyOrSellInfo[] mBuy,
     61             BuyOrSellInfo[] mSell, String mDate, String mTime) {
     62         super();
     63         this.mName = mName;
     64         this.mTodayPrice = mTodayPrice;
     65         this.mYestodayPrice = mYestodayPrice;
     66         this.mNowPrice = mNowPrice;
     67         this.mHighestPrice = mHighestPrice;
     68         this.mLowestPrice = mLowestPrice;
     69         this.mBuy1Price = mBuy1Price;
     70         this.mSell1Price = mSell1Price;
     71         this.mTradeCount = mTradeCount;
     72         this.mTradeMoney = mTradeMoney;
     73         this.mBuy = mBuy;
     74         this.mSell = mSell;
     75         this.mDate = mDate;
     76         this.mTime = mTime;
     77     }
     78 
     79     @Override
     80     public String toString() {
     81         StringBuilder sb = new StringBuilder();
     82         sb.append("股票名称: " + mName + "
    ");
     83         sb.append("今日开盘价: " + mTodayPrice + "元
    ");
     84         sb.append("昨日收盘价: " + mYestodayPrice + "元
    ");
     85         sb.append("当前股价: " + mNowPrice + "元
    ");
     86         sb.append("今日最高价: " + mHighestPrice + "元
    ");
     87         sb.append("今日最低价: " + mLowestPrice + "元
    ");
     88         sb.append("今日交易量: " + mTradeCount + "股
    ");
     89         sb.append("今日成交量: " + mTradeMoney + "元
    ");
     90 
     91         BuyOrSellInfo[] buy = mBuy;
     92         sb.append("买一:
    " + buy[0] + "
    ");
     93         sb.append("买二:
    " + buy[1] + "
    ");
     94         sb.append("买三:
    " + buy[2] + "
    ");
     95         sb.append("买四:
    " + buy[3] + "
    ");
     96         sb.append("买五:
    " + buy[4] + "
    ");
     97 
     98         BuyOrSellInfo[] sell = mSell;
     99         sb.append("卖一:
    " + sell[0] + "
    ");
    100         sb.append("卖二:
    " + sell[1] + "
    ");
    101         sb.append("卖三:
    " + sell[2] + "
    ");
    102         sb.append("卖四:
    " + sell[3] + "
    ");
    103         sb.append("卖五:
    " + sell[4] + "
    ");
    104 
    105         sb.append("时间: " + mTime + "
    ");
    106         sb.append("日期: " + mDate + "
    ");
    107 
    108         return sb.toString();
    109     }
    110 
    111     /** 自定义的转换异常类 */
    112     public static class ParseStockInfoException extends Exception {
    113         public ParseStockInfoException() {
    114             super("Parse StockInfo error!");
    115         }
    116     }
    117 
    118     /**
    119      * http://hq.sinajs.cn/list=sz300114,
    120      * <P>
    121      * 将js数据转化为bean
    122      * 
    123      * @param source
    124      *            的格式为:
    125      *            <p>
    126      *            var hq_str_sz300114=
    127      *            "中航电测,67.930,65.250,61.250,71.500,60.500,61.240,61.250,6962845,453642618.970,4400,61.240,500,61.230,3000,61.220,13900,61.210,36290,61.200,27020,61.250,500,61.260,8080,61.300,3290,61.370,1228,61.380,2015-06-03,15:05:55,00"
    128      *            ;
    129      * @return
    130      * @throws ParseStockInfoException
    131      */
    132     public static SinaStockInfo parseStockInfo(String source)
    133             throws ParseStockInfoException {
    134         source = source.substring(source.indexOf(""") + 1,
    135                 source.lastIndexOf(","));
    136         String[] infoStr = source.split(",");
    137 
    138         if (infoStr.length != 32) {
    139             throw new ParseStockInfoException();
    140         }
    141 
    142         final String name = infoStr[0];
    143         final float todayPrice = Float.parseFloat(infoStr[1]);
    144         final float yestodayPrice = Float.parseFloat(infoStr[2]);
    145         final float nowPrice = Float.parseFloat(infoStr[3]);
    146         final float highestPrice = Float.parseFloat(infoStr[4]);
    147         final float lowestPrice = Float.parseFloat(infoStr[5]);
    148         final float buy1Price = Float.parseFloat(infoStr[6]);
    149         final float sell1Price = Float.parseFloat(infoStr[7]);
    150         final long tradeCount = Long.parseLong(infoStr[8]);
    151         final long tradeMoney = Long.parseLong(infoStr[9]);
    152 
    153         BuyOrSellInfo buy1 = new BuyOrSellInfo(Long.parseLong(infoStr[10]),
    154                 Float.parseFloat(infoStr[11]));
    155         BuyOrSellInfo buy2 = new BuyOrSellInfo(Long.parseLong(infoStr[12]),
    156                 Float.parseFloat(infoStr[13]));
    157         BuyOrSellInfo buy3 = new BuyOrSellInfo(Long.parseLong(infoStr[14]),
    158                 Float.parseFloat(infoStr[15]));
    159         BuyOrSellInfo buy4 = new BuyOrSellInfo(Long.parseLong(infoStr[16]),
    160                 Float.parseFloat(infoStr[17]));
    161         BuyOrSellInfo buy5 = new BuyOrSellInfo(Long.parseLong(infoStr[18]),
    162                 Float.parseFloat(infoStr[19]));
    163 
    164         BuyOrSellInfo sell1 = new BuyOrSellInfo(Long.parseLong(infoStr[20]),
    165                 Float.parseFloat(infoStr[21]));
    166         BuyOrSellInfo sell2 = new BuyOrSellInfo(Long.parseLong(infoStr[22]),
    167                 Float.parseFloat(infoStr[23]));
    168         BuyOrSellInfo sell3 = new BuyOrSellInfo(Long.parseLong(infoStr[24]),
    169                 Float.parseFloat(infoStr[25]));
    170         BuyOrSellInfo sell4 = new BuyOrSellInfo(Long.parseLong(infoStr[26]),
    171                 Float.parseFloat(infoStr[27]));
    172         BuyOrSellInfo sell5 = new BuyOrSellInfo(Long.parseLong(infoStr[28]),
    173                 Float.parseFloat(infoStr[29]));
    174 
    175         final String date = infoStr[30];
    176         final String time = infoStr[31];
    177 
    178         SinaStockInfo stockInfo = new SinaStockInfo(name, todayPrice,
    179                 yestodayPrice, nowPrice, highestPrice, lowestPrice, buy1Price,
    180                 sell1Price, tradeCount, tradeMoney, new BuyOrSellInfo[] { buy1,
    181                         buy2, buy3, buy4, buy5 }, new BuyOrSellInfo[] { sell1,
    182                         sell2, sell3, sell4, sell5 }, date, time);
    183 
    184         return stockInfo;
    185 
    186     }
    187 
    188     /** 买单或卖单信息。 */
    189     static class BuyOrSellInfo {
    190         // 数量。单位为“股”。100股为1手。
    191         long mCount;
    192         // 价格。
    193         float mPrice;
    194 
    195         public BuyOrSellInfo(long count, float price) {
    196             mCount = count;
    197             mPrice = price;
    198         }
    199 
    200         @Override
    201         public String toString() {
    202             StringBuilder sb = new StringBuilder();
    203             sb.append("数量: " + mCount + "股    ");
    204             sb.append("价格: " + mPrice + "元");
    205             return sb.toString();
    206         }
    207     }
    208 }

     2.客户端实例类

      1 import java.io.BufferedInputStream;
      2 import java.io.BufferedReader;
      3 import java.io.IOException;
      4 import java.io.InputStream;
      5 import java.io.InputStreamReader;
      6 import java.io.Reader;
      7 import java.nio.charset.Charset;
      8 import java.util.ArrayList;
      9 import java.util.List;
     10 
     11 import org.apache.commons.httpclient.HttpClient;
     12 import org.apache.commons.httpclient.HttpException;
     13 import org.apache.commons.httpclient.HttpMethod;
     14 import org.apache.commons.httpclient.HttpStatus;
     15 import org.apache.commons.httpclient.methods.GetMethod;
     16 
     17 import com.sina.stock.SinaStockInfo.ParseStockInfoException;
     18 
     19 import android.R.string;
     20 import android.graphics.Bitmap;
     21 import android.graphics.BitmapFactory;
     22 import android.text.TextUtils;
     23 import android.util.Log;
     24 
     25 /**
     26  * 新浪数据获取客户端,提供个股行情查询、大盘指数查询、个股分时图、K线图等基本数据查询服务。
     27  */
     28 public final class SinaStockClient {
     29     
     30     private final static String STOCK_URL = "http://hq.sinajs.cn/list=";
     31     
     32     private final static String STOCK_MINITE_URL = "http://image.sinajs.cn/newchart/min/n/";
     33     private final static String STOCK_DAILY_URL = "http://image.sinajs.cn/newchart/daily/n/";
     34     private final static String STOCK_WEEKLY_URL = "http://image.sinajs.cn/newchart/weekly/n/";
     35     private final static String STOCK_MONTHLY_URL = "http://image.sinajs.cn/newchart/monthly/n/";
     36     
     37     public final static int IMAGE_TYPE_MINITE = 0x85;
     38     public final static int IMAGE_TYPE_DAILY = 0x86;
     39     public final static int IMAGE_TYPE_WEEKLY = 0x87;
     40     public final static int IMAGE_TYPE_MONTHLY = 0x88;
     41     
     42     private final static int CONNECTION_TIMEOUT = 5000;
     43     private final static int SO_TIMEOUT = 30000;
     44     
     45     private HttpClient mHttpClient;
     46     
     47     private static SinaStockClient mInstance;
     48     private SinaStockClient(){
     49         mHttpClient = new HttpClient();
     50         
     51         mHttpClient.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIMEOUT);   
     52         mHttpClient.getHttpConnectionManager().getParams().setSoTimeout(SO_TIMEOUT);  
     53 
     54     }
     55     
     56     /**
     57      * 获取客户端实例。
     58      * 
     59      * @return SinaStockClient
     60      */
     61     public synchronized static SinaStockClient getInstance() {
     62         
     63         if(mInstance != null) {
     64             return mInstance;
     65         }
     66         return new SinaStockClient();
     67     }
     68     
     69     /**
     70      * 通过股票代码,获取行情信息。
     71      * 
     72      * @param stockCodes 
     73      *         股票代码数组。沪市股票代码以"sh+股票代码", 深市股票代码以"sz+股票代码"。
     74      * 
     75      * @return 成功返回List<SinaStockInfo>,失败返回null。 
     76      *         
     77      * @throws IOException 
     78      * @throws HttpException 
     79      * @throws ParseStockInfoException 
     80      */
     81     public List<SinaStockInfo> getStockInfo(String[] stockCodes) throws HttpException, IOException, ParseStockInfoException {
     82         String url = STOCK_URL + generateStockCodeRequest(stockCodes);
     83         
     84         HttpMethod method = new GetMethod(url);
     85         int statusCode = mHttpClient.executeMethod(method);
     86         if(statusCode != HttpStatus.SC_OK) {
     87             method.releaseConnection();
     88             return null;
     89         }
     90         
     91         InputStream is = method.getResponseBodyAsStream();
     92         InputStreamReader reader = new InputStreamReader(new BufferedInputStream(is), Charset.forName("gbk"));
     93         BufferedReader bReader = new BufferedReader(reader);
     94         
     95         List<SinaStockInfo> list = parseSinaStockInfosFromReader(bReader);
     96         bReader.close();
     97         method.releaseConnection();
     98         
     99         return list;
    100     }
    101 
    102     /**
    103      * 获取股票分时图或K线图。
    104      * 
    105      * @param stockCode
    106      *         股票代码。沪市股票代码以"sh+股票代码", 深市股票代码以"sz+股票代码"。
    107      * @param imageType
    108      *         指明请求的图像类型。
    109      *         IMAGE_TYPE_MINITE 为分时图。
    110      *         IMAGE_TYPE_DAILY 为日K线图。
    111      *         IMAGE_TYPE_WEEKLY 为周K线图。
    112      *         IMAGE_TYPE_MONTHLY 为月K线图。
    113      * @return 如果成功则返回Bitmap图像,失败返回null。
    114      * @throws IOException 
    115      * @throws HttpException 
    116      */
    117     public Bitmap getStockImage(String stockCode, int imageType) throws HttpException, IOException {
    118         String baseRequestUrl = null;
    119         switch(imageType) {
    120         case IMAGE_TYPE_MINITE:
    121             baseRequestUrl = STOCK_MINITE_URL;
    122             break;
    123         case IMAGE_TYPE_DAILY:
    124             baseRequestUrl = STOCK_DAILY_URL;
    125             break;
    126         case IMAGE_TYPE_WEEKLY:
    127             baseRequestUrl = STOCK_WEEKLY_URL;
    128             break;
    129         case IMAGE_TYPE_MONTHLY:
    130             baseRequestUrl = STOCK_MONTHLY_URL;
    131             break;
    132         }
    133         
    134         if(TextUtils.isEmpty(baseRequestUrl)) {
    135             return null;
    136         }
    137         
    138         String fullRequestUrl = baseRequestUrl + stockCode + ".gif";
    139         
    140         return getBitmapFromUrl(fullRequestUrl);
    141     }
    142     
    143     private String generateStockCodeRequest(String[] stockCodes){
    144         
    145         if(stockCodes == null || stockCodes.length == 0) {
    146             return null;
    147         }
    148         
    149         StringBuilder sb = new StringBuilder(stockCodes[0]);
    150         final int length = stockCodes.length;
    151         
    152         for(int i = 1; i != length; ++i) {
    153             sb.append(',');
    154             sb.append(stockCodes[i]);
    155         }
    156         
    157         return sb.toString();
    158     }
    159 
    160     private List<SinaStockInfo> parseSinaStockInfosFromReader(BufferedReader reader) throws IOException, ParseStockInfoException {
    161         
    162         ArrayList<SinaStockInfo> list = new ArrayList<SinaStockInfo>(10);
    163         String sourceLine = null;
    164         
    165         while((sourceLine = reader.readLine()) != null) {
    166             list.add(SinaStockInfo.parseStockInfo(sourceLine));
    167         }
    168         
    169         return list;
    170     }
    171 
    172     private Bitmap getBitmapFromUrl(String url) throws HttpException, IOException {
    173         
    174         HttpMethod method = new GetMethod(url);
    175         int statusCode = mHttpClient.executeMethod(method);
    176         if(statusCode != HttpStatus.SC_OK) {
    177             method.releaseConnection();
    178             return null;
    179         }
    180         
    181         InputStream in = method.getResponseBodyAsStream();
    182         BufferedInputStream bis = new BufferedInputStream(in);
    183         
    184         Bitmap bm = BitmapFactory.decodeStream(bis);
    185         
    186         bis.close();
    187         method.releaseConnection();
    188         
    189         return bm;
    190     }
    191 }

    参考 http://www.blogjava.net/zh-weir/archive/2012/03/01/371105.html

  • 相关阅读:
    pip命令提示unknow or unsupported command install解决方法
    SSH登录慢解方案
    python/shell脚本报异常^M: bad interpreter: No such file or directory
    MySQL中自增ID起始值修改方法
    Centos给文件设置了777权限仍不能访问解决方案
    CentOS7下安装mysql5.7
    CentOS7下使用yum安装MariaDB
    sublime text 3启动报错"swallow_startup_errors"解决方法
    一个清除数组的方法在 Kotlin、Java、C#和Nim上的性能测试
    国外立交桥设计参考资料
  • 原文地址:https://www.cnblogs.com/Westfalen/p/4549891.html
Copyright © 2011-2022 走看看