① 請問怎樣用Java獲取股票行情歷史數據新浪、搜狐、百度財經都可以......
publicclassStockConnection{
publicstaticvoidmain(String[]args){
URLur=null;
try{
//搜狐股票行情歷史介面
// ur=newURL("http://q.stock.sohu.com/hisHq?code=cn_300228&start=20130930&end=20131231&stat=1&order=D&period=d&callback=historySearchHandler&rt=jsonp");
//新浪股票行情歷史介面
ur=newURL("http://biz.finance.sina.com.cn/stock/flash_hq/kline_data.php?&rand=random(10000)&symbol=sh600000&end_date=20150809&begin_date=20000101&type=plain");
HttpURLConnectionuc=(HttpURLConnection)ur.openConnection();
BufferedReaderreader=newBufferedReader(newInputStreamReader(ur.openStream(),"GBK"));
Stringline;
while((line=reader.readLine())!=null){
System.out.println(line);
}
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
② 新浪JAVA API返回的JSON數據裡面中文亂碼
這是unicode編碼,不是亂碼,你進行Unicode轉碼就出來了
給你寫個方法轉碼,將unicode傳遞進去返回字元串
publicStringconvert(StringutfString){
StringBuildersb=newStringBuilder();
inti=-1;
intpos=0;
while((i=utfString.indexOf("\u",pos))!=-1){
sb.append(utfString.substring(pos,i));
if(i+5<utfString.length()){
pos=i+6;
sb.append((char)Integer.parseInt(utfString.substring(i+2,i+6),16));
}
}
returnsb.toString();
}