Java URL抓住计时器上的股票报价

时间:2012-11-15 17:54:02

标签: java url timer finance

有人建议我在stackoverflow而不是stackexchange中发布这个,所以我在这里。我正在尝试制作一个简单的股票代码。只是尝试空闲时间。无论如何,我试图每5秒(或任何时间)运行以下main()部分:

此代码有效:

import java.net.*;
import java.io.*;

public class URLConnectionReader {
public static void main(String[] args) throws Exception {
String[] stocks={"GOOG","MSFT"}; //
    URL yahoofinance = new URL("http://finance.yahoo.com/d/quotes.csv? 
s="+stocks[0]+"+"+stocks[1]+"&f=hg");
    URLConnection yc = yahoofinance.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(
                                yc.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);
          in.close();
}
}

现在,我已尝试将上述内容纳入下面的时间程序中。尝试过抛出异常并玩try-catch,但此时我已经脱离了我的元素。让我知道。

import java.util.Timer;
import java.util.TimerTask;
import java.net.*;
import java.io.*;

public class StockPrinter {

public static void main(String[] args) throws Exception {
    //1- Taking an instance of Timer class.
    Timer timer = new Timer("Printer");

    //2- Taking an instance of class contains your repeated method.
    MyTask t = new MyTask();


    //TimerTask is a class implements Runnable interface so
    //You have to override run method with your certain code black

    //Second Parameter is the specified the Starting Time for your timer in
    //MilliSeconds or Date

    //Third Parameter is the specified the Period between consecutive
    //calling for the method.

    timer.schedule(t, 0, 2000);

}
}

class MyTask extends TimerTask {  /*extends implies that MyTask has all    
variables/properties of TimerTask */
//times member represent calling times.
private int times = 0;


public void run() {
   // String[] stocks={"GOOG","MSFT"};
    times++;


    if (times <= 5) {

 String[] stocks={"GOOG","MSFT"}; //
    URL yahoofinance = new URL("http://finance.yahoo.com/d/quotes.csv?        

s="+stocks[0]+"+"+stocks[1]+"&f=hg");
     URLConnection yc = yahoofinance.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(
                                yc.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);
          in.close();        

    } else {
        System.out.println("Timer stops now...");

        //Stop Timer.
        this.cancel();
        System.exit(0); //added:should quit program

    }
}
}

注意:我从Java的网站或者某些论坛中抓取了这些代码的主要部分,所以如果有任何一个被识别,我会道歉。没有以任何方式将重物脱掉。只是想让这个工作。以下是编译错误:

StockPrinter.java:43: unreported exception java.net.MalformedURLException; must
be caught or declared to be thrown
    URL yahoofinance = new URL("http://finance.yahoo.com/d/quotes.csv?s="+st
ocks[0]+"+"+stocks[1]+"&f=hg");
                       ^
StockPrinter.java:44: unreported exception java.io.IOException; must be caught o
r declared to be thrown
    URLConnection yc = yahoofinance.openConnection();
                                                  ^
StockPrinter.java:46: unreported exception java.io.IOException; must be caught o
r declared to be thrown
                                yc.getInputStream()));
                                                 ^
StockPrinter.java:48: unreported exception java.io.IOException; must be caught o
r declared to be thrown
    while ((inputLine = in.readLine()) != null)
                                   ^
StockPrinter.java:50: unreported exception java.io.IOException; must be caught o
r declared to be thrown
          in.close();
                  ^
5 errors

2 个答案:

答案 0 :(得分:4)

这对我有用:

        try
        {
            String[] stocks =
            {
                "GOOG", "MSFT"
            }; //
            URL yahoofinance = new URL("http://finance.yahoo.com/d/quotes.csv?s=" + stocks[0] + "+" + stocks[1] + "&f=hg");
            URLConnection yc = yahoofinance.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    yc.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null)
            {
                System.out.println(inputLine);
            }
            in.close();
        } catch (IOException ex)
        {
            System.out.println("Oops bad things happens");
        }

答案 1 :(得分:2)

您在“MyTask”中定义的run方法不会抛出任何异常。 但是,它执行的代码会抛出异常,例如“new URL()”。

它在Main中工作的原因是因为它可以重新抛出任何异常。

由于这些异常未得到处理,编译器(正确地)抱怨它。

您需要使用try / catch块包围违规调用。 例如:

URL yahoofinance = null;
try{
  yahoofinance = new URL( "http://finance.yahoo.com/d/quotes.csv?" );
} catch( MalformedURLException ex ) {
  // print error or throw a new error or whatever
  throw new Error( "I cannot deal with this problem" );
}

为帖子中的每个编译例外执行类似的操作。