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

05-11 20:47