有人建议我将其发布到 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
最佳答案
这对我有用:
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");
}
关于Java URL 在计时器上抓取股票报价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13403334/