找了一下,暂定用新浪的接口,现阶段足够了
接口为:http://hq.sinajs.cn/list=标识股票代码
标识为sh沪市,sz深市,s_sh沪简易信息,s_sz深简易信息。
例如http://hq.sinajs.cn/list=sh600875
返回var hq_str_sh600875="东方电气,12.48,-0.32,-2.50,135125,17091,........................";
以,分隔,需要的只有前面6个:
0 股票名
1 今日开盘价;
2 昨日收盘价;
3 当前价格;
4 今日最高价;
5 今日最低价
传回的值整个转成String,再使用public String[] split(String regex)方法分割,split()方法简单说就是根据regex来分割比如"DF,GG+HH"按",'"就分割成"DF" 和 "GG+HH"。
简单写了个提取的代码
package test;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.InputStream;
public class AccessNI {
final String NULLRES = "无返回值!";
public static void main(String[] args) {
// TODO Auto-generated method stub
final AccessNI ani = new AccessNI();
new Thread() {
public void run() {
String result = ani.accessByGet();
ani.splitString(result);
}
}.start();
}
public void splitString(String result) {
if (!result.equals(NULLRES)) {
String[] a = new String[6];
String[] var = result.split("=\"");
String s = var[1];
String[] as = s.split(",");
for (int i = 0; i < a.length; i++) {
a[i] = as[i];
System.out.println(a[i]);
}
}
}
public String accessByGet() {
// 提交至服务器
// 拼装路径
try {
String path = "http://hq.sinajs.cn/list=sh600875";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
// 取得返回的状态码
int code = conn.getResponseCode();
if (code == 200) {
// 请求成功
InputStream is = conn.getInputStream();
String text = StreamTools.readInputStream(is);
return text;
} else {
return NULLRES;
}
} catch (Exception e) {
e.printStackTrace();
}
return NULLRES;
}
}