我一直在收集項目的股票市場數據,並且在過去幾個月一直這樣做。然而,就在幾天前,我一直用來從雅虎下載數據的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