andoid下的股票行情开发
1.信息类
public class SinaStockInfo { /**
* Sina股票数据接口 以大秦铁路(股票代码:601006)为例,如果要获取它的最新行情,只需访问新浪的股票数据
* 接口:http://hq.sinajs.cn/list=sh601006这个url会返回一串文本,例如:
*
* var hq_str_sh601006="大秦铁路, 27.55, 27.25, 26.91, 27.55, 26.20, 26.91,
* 26.92, 22114263, 589824680, 4695, 26.91, 57590, 26.90, 14700, 26.89,
* 14300, 26.88, 15100, 26.87, 3100, 26.92, 8900, 26.93, 14230, 26.94,
* 25150, 26.95, 15220, 26.96, 2008-01-11, 15:05:32";
*
* 这个字符串由许多数据拼接在一起,不同含义的数据用逗号隔开了,按照程序员的思路,顺序号从0开始。 0:”大秦铁路”,股票名字;
* 1:”27.55″,今日开盘价; 2:”27.25″,昨日收盘价; 3:”26.91″,当前价格; 4:”27.55″,今日最高价;
* 5:”26.20″,今日最低价; 6:”26.91″,竞买价,即“买一”报价; 7:”26.92″,竞卖价,即“卖一”报价;
* 8:”22114263″,成交的股票数,由于股票交易以一百股为基本单位,所以在使用时,通常把该值除以一百;
* 9:”589824680″,成交金额,单位为“元”,为了一目了然,通常以“万元”为成交金额的单位,所以通常把该值除以一万;
* 10:”4695″,“买一”申请4695股,即47手; 11:”26.91″,“买一”报价; 12:”57590″,“买二”
* 13:”26.90″,“买二” 14:”14700″,“买三” 15:”26.89″,“买三” 16:”14300″,“买四”
* 17:”26.88″,“买四” 18:”15100″,“买五” 19:”26.87″,“买五”
* 20:”3100″,“卖一”申报3100股,即31手; 21:”26.92″,“卖一”报价 (22, 23), (24, 25),
* (26,27), (28, 29)分别为“卖二”至“卖四的情况” 30:”2008-01-11″,日期; 31:”15:05:32″,时间;
*/ // 股票名字
public String mName;
// 今日开盘价
public float mTodayPrice;
// 昨日收盘价
public float mYestodayPrice;
// 当前价
public float mNowPrice;
// 今日最高价
public float mHighestPrice;
// 今日最低价
public float mLowestPrice;
// 买一价
public float mBuy1Price;
// 卖一价
public float mSell1Price;
// 成交股票数,单位“股”。100股为1手。
public long mTradeCount;
// 成交额,单位“元”。一般需要转换成“万元”。
public long mTradeMoney;
// 买单情况
public BuyOrSellInfo[] mBuy;
// 卖单情况
public BuyOrSellInfo[] mSell;
// 日期
public String mDate;
// 时间
public String mTime; // 涨跌 = 现价 - 昨收 ;
// 涨幅 = 涨跌 / 昨收 ;
// 振幅 = (今日最高价 - 今日最低价)/昨收 private SinaStockInfo(String mName, float mTodayPrice,
float mYestodayPrice, float mNowPrice, float mHighestPrice,
float mLowestPrice, float mBuy1Price, float mSell1Price,
long mTradeCount, long mTradeMoney, BuyOrSellInfo[] mBuy,
BuyOrSellInfo[] mSell, String mDate, String mTime) {
super();
this.mName = mName;
this.mTodayPrice = mTodayPrice;
this.mYestodayPrice = mYestodayPrice;
this.mNowPrice = mNowPrice;
this.mHighestPrice = mHighestPrice;
this.mLowestPrice = mLowestPrice;
this.mBuy1Price = mBuy1Price;
this.mSell1Price = mSell1Price;
this.mTradeCount = mTradeCount;
this.mTradeMoney = mTradeMoney;
this.mBuy = mBuy;
this.mSell = mSell;
this.mDate = mDate;
this.mTime = mTime;
} @Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("股票名称: " + mName + "\n");
sb.append("今日开盘价: " + mTodayPrice + "元\n");
sb.append("昨日收盘价: " + mYestodayPrice + "元\n");
sb.append("当前股价: " + mNowPrice + "元\n");
sb.append("今日最高价: " + mHighestPrice + "元\n");
sb.append("今日最低价: " + mLowestPrice + "元\n");
sb.append("今日交易量: " + mTradeCount + "股\n");
sb.append("今日成交量: " + mTradeMoney + "元\n"); BuyOrSellInfo[] buy = mBuy;
sb.append("买一:\n" + buy[0] + "\n");
sb.append("买二:\n" + buy[1] + "\n");
sb.append("买三:\n" + buy[2] + "\n");
sb.append("买四:\n" + buy[3] + "\n");
sb.append("买五:\n" + buy[4] + "\n"); BuyOrSellInfo[] sell = mSell;
sb.append("卖一:\n" + sell[0] + "\n");
sb.append("卖二:\n" + sell[1] + "\n");
sb.append("卖三:\n" + sell[2] + "\n");
sb.append("卖四:\n" + sell[3] + "\n");
sb.append("卖五:\n" + sell[4] + "\n"); sb.append("时间: " + mTime + "\n");
sb.append("日期: " + mDate + "\n"); return sb.toString();
} /** 自定义的转换异常类 */
public static class ParseStockInfoException extends Exception {
public ParseStockInfoException() {
super("Parse StockInfo error!");
}
} /**
* http://hq.sinajs.cn/list=sz300114,
* <P>
* 将js数据转化为bean
*
* @param source
* 的格式为:
* <p>
* var hq_str_sz300114=
* "中航电测,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"
* ;
* @return
* @throws ParseStockInfoException
*/
public static SinaStockInfo parseStockInfo(String source)
throws ParseStockInfoException {
source = source.substring(source.indexOf("\"") + 1,
source.lastIndexOf(","));
String[] infoStr = source.split(","); if (infoStr.length != 32) {
throw new ParseStockInfoException();
} final String name = infoStr[0];
final float todayPrice = Float.parseFloat(infoStr[1]);
final float yestodayPrice = Float.parseFloat(infoStr[2]);
final float nowPrice = Float.parseFloat(infoStr[3]);
final float highestPrice = Float.parseFloat(infoStr[4]);
final float lowestPrice = Float.parseFloat(infoStr[5]);
final float buy1Price = Float.parseFloat(infoStr[6]);
final float sell1Price = Float.parseFloat(infoStr[7]);
final long tradeCount = Long.parseLong(infoStr[8]);
final long tradeMoney = Long.parseLong(infoStr[9]); BuyOrSellInfo buy1 = new BuyOrSellInfo(Long.parseLong(infoStr[10]),
Float.parseFloat(infoStr[11]));
BuyOrSellInfo buy2 = new BuyOrSellInfo(Long.parseLong(infoStr[12]),
Float.parseFloat(infoStr[13]));
BuyOrSellInfo buy3 = new BuyOrSellInfo(Long.parseLong(infoStr[14]),
Float.parseFloat(infoStr[15]));
BuyOrSellInfo buy4 = new BuyOrSellInfo(Long.parseLong(infoStr[16]),
Float.parseFloat(infoStr[17]));
BuyOrSellInfo buy5 = new BuyOrSellInfo(Long.parseLong(infoStr[18]),
Float.parseFloat(infoStr[19])); BuyOrSellInfo sell1 = new BuyOrSellInfo(Long.parseLong(infoStr[20]),
Float.parseFloat(infoStr[21]));
BuyOrSellInfo sell2 = new BuyOrSellInfo(Long.parseLong(infoStr[22]),
Float.parseFloat(infoStr[23]));
BuyOrSellInfo sell3 = new BuyOrSellInfo(Long.parseLong(infoStr[24]),
Float.parseFloat(infoStr[25]));
BuyOrSellInfo sell4 = new BuyOrSellInfo(Long.parseLong(infoStr[26]),
Float.parseFloat(infoStr[27]));
BuyOrSellInfo sell5 = new BuyOrSellInfo(Long.parseLong(infoStr[28]),
Float.parseFloat(infoStr[29])); final String date = infoStr[30];
final String time = infoStr[31]; SinaStockInfo stockInfo = new SinaStockInfo(name, todayPrice,
yestodayPrice, nowPrice, highestPrice, lowestPrice, buy1Price,
sell1Price, tradeCount, tradeMoney, new BuyOrSellInfo[] { buy1,
buy2, buy3, buy4, buy5 }, new BuyOrSellInfo[] { sell1,
sell2, sell3, sell4, sell5 }, date, time); return stockInfo; } /** 买单或卖单信息。 */
static class BuyOrSellInfo {
// 数量。单位为“股”。100股为1手。
long mCount;
// 价格。
float mPrice; public BuyOrSellInfo(long count, float price) {
mCount = count;
mPrice = price;
} @Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("数量: " + mCount + "股 ");
sb.append("价格: " + mPrice + "元");
return sb.toString();
}
}
}
2.客户端实例类
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod; import com.sina.stock.SinaStockInfo.ParseStockInfoException; import android.R.string;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Log; /**
* 新浪数据获取客户端,提供个股行情查询、大盘指数查询、个股分时图、K线图等基本数据查询服务。
*/
public final class SinaStockClient { private final static String STOCK_URL = "http://hq.sinajs.cn/list="; private final static String STOCK_MINITE_URL = "http://image.sinajs.cn/newchart/min/n/";
private final static String STOCK_DAILY_URL = "http://image.sinajs.cn/newchart/daily/n/";
private final static String STOCK_WEEKLY_URL = "http://image.sinajs.cn/newchart/weekly/n/";
private final static String STOCK_MONTHLY_URL = "http://image.sinajs.cn/newchart/monthly/n/"; public final static int IMAGE_TYPE_MINITE = 0x85;
public final static int IMAGE_TYPE_DAILY = 0x86;
public final static int IMAGE_TYPE_WEEKLY = 0x87;
public final static int IMAGE_TYPE_MONTHLY = 0x88; private final static int CONNECTION_TIMEOUT = 5000;
private final static int SO_TIMEOUT = 30000; private HttpClient mHttpClient; private static SinaStockClient mInstance;
private SinaStockClient(){
mHttpClient = new HttpClient(); mHttpClient.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIMEOUT);
mHttpClient.getHttpConnectionManager().getParams().setSoTimeout(SO_TIMEOUT); } /**
* 获取客户端实例。
*
* @return SinaStockClient
*/
public synchronized static SinaStockClient getInstance() { if(mInstance != null) {
return mInstance;
}
return new SinaStockClient();
} /**
* 通过股票代码,获取行情信息。
*
* @param stockCodes
* 股票代码数组。沪市股票代码以"sh+股票代码", 深市股票代码以"sz+股票代码"。
*
* @return 成功返回List<SinaStockInfo>,失败返回null。
*
* @throws IOException
* @throws HttpException
* @throws ParseStockInfoException
*/
public List<SinaStockInfo> getStockInfo(String[] stockCodes) throws HttpException, IOException, ParseStockInfoException {
String url = STOCK_URL + generateStockCodeRequest(stockCodes); HttpMethod method = new GetMethod(url);
int statusCode = mHttpClient.executeMethod(method);
if(statusCode != HttpStatus.SC_OK) {
method.releaseConnection();
return null;
} InputStream is = method.getResponseBodyAsStream();
InputStreamReader reader = new InputStreamReader(new BufferedInputStream(is), Charset.forName("gbk"));
BufferedReader bReader = new BufferedReader(reader); List<SinaStockInfo> list = parseSinaStockInfosFromReader(bReader);
bReader.close();
method.releaseConnection(); return list;
} /**
* 获取股票分时图或K线图。
*
* @param stockCode
* 股票代码。沪市股票代码以"sh+股票代码", 深市股票代码以"sz+股票代码"。
* @param imageType
* 指明请求的图像类型。
* IMAGE_TYPE_MINITE 为分时图。
* IMAGE_TYPE_DAILY 为日K线图。
* IMAGE_TYPE_WEEKLY 为周K线图。
* IMAGE_TYPE_MONTHLY 为月K线图。
* @return 如果成功则返回Bitmap图像,失败返回null。
* @throws IOException
* @throws HttpException
*/
public Bitmap getStockImage(String stockCode, int imageType) throws HttpException, IOException {
String baseRequestUrl = null;
switch(imageType) {
case IMAGE_TYPE_MINITE:
baseRequestUrl = STOCK_MINITE_URL;
break;
case IMAGE_TYPE_DAILY:
baseRequestUrl = STOCK_DAILY_URL;
break;
case IMAGE_TYPE_WEEKLY:
baseRequestUrl = STOCK_WEEKLY_URL;
break;
case IMAGE_TYPE_MONTHLY:
baseRequestUrl = STOCK_MONTHLY_URL;
break;
} if(TextUtils.isEmpty(baseRequestUrl)) {
return null;
} String fullRequestUrl = baseRequestUrl + stockCode + ".gif"; return getBitmapFromUrl(fullRequestUrl);
} private String generateStockCodeRequest(String[] stockCodes){ if(stockCodes == null || stockCodes.length == 0) {
return null;
} StringBuilder sb = new StringBuilder(stockCodes[0]);
final int length = stockCodes.length; for(int i = 1; i != length; ++i) {
sb.append(',');
sb.append(stockCodes[i]);
} return sb.toString();
} private List<SinaStockInfo> parseSinaStockInfosFromReader(BufferedReader reader) throws IOException, ParseStockInfoException { ArrayList<SinaStockInfo> list = new ArrayList<SinaStockInfo>(10);
String sourceLine = null; while((sourceLine = reader.readLine()) != null) {
list.add(SinaStockInfo.parseStockInfo(sourceLine));
} return list;
} private Bitmap getBitmapFromUrl(String url) throws HttpException, IOException { HttpMethod method = new GetMethod(url);
int statusCode = mHttpClient.executeMethod(method);
if(statusCode != HttpStatus.SC_OK) {
method.releaseConnection();
return null;
} InputStream in = method.getResponseBodyAsStream();
BufferedInputStream bis = new BufferedInputStream(in); Bitmap bm = BitmapFactory.decodeStream(bis); bis.close();
method.releaseConnection(); return bm;
}
}
参考http://www.blogjava.net/zh-weir/archive/2012/03/01/371105.html
andoid下的股票行情开发的更多相关文章
- iOS 开发 - 改善APP的流畅度 (绘制股票行情)
这几天做了一个查看股票行情的app. 完成之后,当k线比较多的时候,app 对于捏合.拖动手势的反应不太流畅, 主要原因是drawRect: 干的活太多, 竟然需要40ms+, fps 自然不高 最后 ...
- canvas绘图,html5 k线图,股票行情图
canvas绘图,html5 k线图,股票行情图 canvas跟其他标签一样,也可以通过css来定义样式.但这里需要注意的是:canvas的默认宽高为300px * 150px,在css中为canva ...
- Windows下搭建Spark+Hadoop开发环境
Windows下搭建Spark+Hadoop开发环境需要一些工具支持. 只需要确保您的电脑已装好Java环境,那么就可以开始了. 一. 准备工作 1. 下载Hadoop2.7.1版本(写Spark和H ...
- Linux学习心得之 Linux下命令行Android开发环境的搭建
作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Linux学习心得之 Linux下命令行Android开发环境的搭建 1. 前言2. Jav ...
- 【转】linux和windows下安装python集成开发环境及其python包
本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...
- 【Unity3D游戏开发】NGUI之多分辨率下完美分布式协同开发 (五)
NGUI多分辨率下完美分布式协同开发:不同分辨率下相对于屏幕坐标的Perfab数据不再丢失 NGUI多分辨率下完美分布式协同开发不同分辨率下相对于屏幕坐标的Perfab数据不再丢失 开发问题 原因分析 ...
- mac10.9下eclipse的storm开发环境搭建
--------------------------------------- 博文作者:迦壹 博客地址:http://idoall.org/home.php?mod=space&uid=1& ...
- Ubuntu下配置C/C++开发环境
在 Ubuntu 下配置 C/C++ 开发环境 转自:白巴的临时空间 Submitted by 白巴 on 2009-04-27 19:52:12. 学习笔记 虽然 Ubuntu 的版本已经是9.04 ...
- Windows下的Objective-C集成开发环境(IDE)
Windows下的Objective-C集成开发环境(IDE) 分类: Objective-C2012-04-20 21:54 26631人阅读 评论(42) 收藏 举报 windowside编译器c ...
随机推荐
- A股市场各行业龙头股一览表
A股市场各行业龙头股一览表 一.指标股: 工商银行.中国银行.中国石化.中国国航.宝钢股份. 中国神华.建设银行.招商银行.华能国际.中国联通. 长江电力.中国人寿.中国石油 二.金融: 招商银行.浦 ...
- ELK-Python(三)
不具有通用性,留作纪念. [root@GXB-CTRLCENTER python]# cat insert_uv.py #!/usr/bin/env python # -*- coding:utf-8 ...
- Linux是怎么启动的
按下电源按钮的直到欢迎页出来之后,linux总共做的事可以分为五步来完成. 1. BIOS加电自检: 加电自检,检测硬件设备.然后按照cmos上面的顺序来搜索处在活动状态下的可以引导的设备.可以是光驱 ...
- SSH项目整合教学Eclipse搭建SSH(Struts2+Spring3+Hibernate3)
这篇博文的目的 尝试搭建一个完整的SSH框架项目. 给以后的自己,也给别人一个参考. 读博文前应该注意: 本文提纲:本文通过一个用户注册的实例讲解SSH的整合.创建Struts项目,整合Hiberna ...
- [BS-11] 关于RGB/ARGB颜色相关知识
关于RGB/ARGB颜色相关知识 众所周知,自然界的颜色都是由红色R.绿色G.蓝色B三元色按不同比例混合而成,每种元色取值范围是0-255.iOS中图片的颜色分为2种:24位和32位. 1. 24位( ...
- The Struts dispatcher cannot be found. This is usually caused by using Strut
The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the assoc ...
- Java Axis2 1.6.3+JDK1.7.0_13+Tomcat7.0.65+eclipse搭建web service
安装文件下载: jdk1.7.0_13 安装步骤参考文章:http://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html tomcat7. ...
- NHibernate 3.2+的 Map by Code 实例
NHibernate 3.2+的 Map by Code 的文档真的很少,除了Fabio Maulo的2篇blog(顺带说李永京的翻译,没任何新的研究). 让人感慨NH的没落, 也许从没有流行过. 在 ...
- 配置suse自动化安装
配置suse自动化安装 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 前言:不知道你习惯用那款虚拟器,我用的是VMware Workstation,别问我为什么,因 ...
- C# 微信支付教程系列之扫码支付
微信支付教程系列之扫码支付 今天,我们来一起探讨一下这个微信扫码支付.何为扫码支付呢?这里面,扫的码就是二维码了,就是我们经常扫一扫的那种二维码图片,例如,我们自己添加好友的时候 ...