Newer
Older
stockTray / StockTray_old / src / HttpUtil.java
ubt on 25 May 2018 3 KB stock new
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class HttpUtil {

	/**
	 * 正常访问股票数据(重复请求)
	 * 
	 * @param stockID
	 * @return
	 * @throws IOException
	 */
	public static ArrayList<StockBean> StockHttp(String stockID)
			throws IOException {

		String result = httpRequest(stockID);

		if (result.length() > 0) {
			return getCurrentStock(result);
		} else {
			return null;
		}

	}

	/**
	 * 判断股票代码是否存在 返回正确的代码
	 * 
	 * @param code
	 * @return
	 * @throws IOException
	 */
	public static String verifyCode(String code) {
		try {

			String result1 = httpRequest(code);
			if (!result1.contains("FAILED") && !result1.endsWith("=")) {
				return code;
			}

			String result2 = httpRequest("sh" + code);
			if (!result2.contains("FAILED") && !result2.endsWith("=")) {
				return "sh" + code;
			}

			String result3 = httpRequest("sz" + code);
			if (!result3.contains("FAILED") && !result3.endsWith("=")) {
				return "sz" + code;
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;

	}

	/**
	 * 处理网页返回的股票数据
	 * 
	 * @param result
	 * @return
	 */
	public static ArrayList<StockBean> getCurrentStock(String result) {
		// sz000838=名称,10.690,10.980,10.680,10.790,10.390,10.670,10.680,34446889,364500619.310,13700,10.670,44600,10.660,44300,10.650,19100,10.640,58700,10.630,24900,10.680,94963,10.690,210232,10.700,19600,10.710,58900,10.720,2016-10-24,13:51:36,00
		try {

			String[] codeS = result.split("=");
			String[] infoS = codeS[1].split(",");

			ArrayList<StockBean> array = new ArrayList<StockBean>();
			StockBean bean = new StockBean();
			bean.setCode(codeS[0]);
			bean.setName(infoS[0]);
			bean.setTdPrice(infoS[1]);
			bean.setYtdPrice(infoS[2]);
			bean.setCurPrice(infoS[3]);
			bean.setMaxPrice(infoS[4]);
			bean.setMinPrice(infoS[5]);
			bean.setBuyPrice(infoS[6]);
			bean.setSalePrice(infoS[7]);
			bean.setDealCount(infoS[8]);
			bean.setDealMoney(infoS[9]);
			bean.setBuy1Count(infoS[10]);
			bean.setBuy1Price(infoS[11]);
			bean.setBuy2Count(infoS[12]);
			bean.setBuy2Price(infoS[13]);
			bean.setBuy3Count(infoS[14]);
			bean.setBuy3Price(infoS[15]);
			bean.setBuy4Count(infoS[16]);
			bean.setBuy4Price(infoS[17]);
			bean.setBuy5Count(infoS[18]);
			bean.setBuy5Price(infoS[19]);
			bean.setSale1Count(infoS[20]);
			bean.setSale1Price(infoS[21]);
			bean.setSale2Count(infoS[22]);
			bean.setSale2Price(infoS[23]);
			bean.setSale3Count(infoS[24]);
			bean.setSale3Price(infoS[25]);
			bean.setSale4Count(infoS[26]);
			bean.setSale4Price(infoS[27]);
			bean.setSale5Count(infoS[28]);
			bean.setSale5Price(infoS[29]);
			bean.setDate(infoS[30]);
			bean.setTime(infoS[31]);

			array.add(bean);

			return array;

		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 请求网页内容
	 * 
	 * @param stockID
	 * @return
	 * @throws IOException
	 */
	public static String httpRequest(String stockID) throws IOException {
		URL url = new URL("http://hq.sinajs.cn/format=text&list=" + stockID); // sz000838

		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		InputStream inputStream = connection.getInputStream();
		InputStreamReader isr = new InputStreamReader(inputStream, "GB2312");// 添加这一句话设置相应编码格式

		BufferedReader br = new BufferedReader(isr);
		String temp = null;
		String result = "";
		while ((temp = br.readLine()) != null) {
			result += temp;
		}
		return result;
	}
}