我一直在收集项目的股票市场数据,并且在过去几个月一直这样做。然而,就在几天前,我一直用来从雅虎下载数据的URLConnection已经为空,尽管没有做任何修改,并且在此之前它已经完美运行。来自雅虎的URL下载一个我阅读并打印出来的csv文件,并且如果我在浏览器上访问该URL,该URL是有效的,然后当我在我的代码中连接到它时,URLConnection为空。尝试与其他股票也没有帮助。Java URLConnection对于有效的URL返回null
private URL url = null;
private URLConnection urlConn = null;
private InputStreamReader inStream = null;
try {
//http://ichart.finance.yahoo.com/table.csv?s=^FTSE&d=3&e=18&f=2017&g=d&a=1&b=14&c=2017
this.url = new URL(urlStr);
System.out.println(url.toString());
this.urlConn = url.openConnection();
this.urlConn.connect();
//Start Reading
this.inStream = new InputStreamReader(this.urlConn.getInputStream());
BufferedReader buff= new BufferedReader(this.inStream);
System.out.println(buff.readLine());
}catch (MalformedURLException e) {
System.out.println(e.getMessage());
}catch(IOException e){
System.out.println(e.getMessage());
}
将IOException更改为Exception以查看是否缺少应该捕获的异常。这可能是一个403错误。 – Sedrick
'URLConnection'是* not * null。 'readLine()'*返回* null,或者抛出异常。准确。 – EJP