您的位置:首页 > 编程语言 > Java开发

如何使用java获取雅虎财经上的股票信息

2013-04-01 10:33 405 查看
public void retrievePriceInfo(){

        try {

            URL priceInfo=new URL(PRICE_URL);

            

            //open the URL connection to retrieve the price info.

            InputStream in= priceInfo.openConnection().getInputStream();

            

            if (in==null) {

                logger.error("Open URL connection failed");

            }

            

            //get the current time.

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            this.setCurDate(df.format(new Date()));

            

            //parse the retrieved data to get the price info.

            try {

                this.setPrice(IOUtils.toString(in,"UTF-8"));

            } catch(Exception e) {

                logger.error("parse price info failed."+e.getMessage());

                e.printStackTrace();

                return ;

            }

            

            logger.warn("price is: "+this.getPrice());

            logger.warn("current time is: "+ curDate);

            

            storPrice();

            

        } catch (MalformedURLException e) {

            logger.error(" Either no legal protocol could be found in a specification string or the string could not be parsed:"+e.getMessage());

            e.printStackTrace();

            return ;

        } catch (IOException e) {

            logger.error("Open URL connection failed"+e.getMessage());

            e.printStackTrace();

            return ;

        }

        

        return ;

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: