如何將網站的結果(例如https://blockchain.info/de/q/24hrprice)轉換爲字符串/整數?我必須從網站上閱讀源代碼嗎?從網站到字符串的結果
我正在開發Android Studio。
如何將網站的結果(例如https://blockchain.info/de/q/24hrprice)轉換爲字符串/整數?我必須從網站上閱讀源代碼嗎?從網站到字符串的結果
我正在開發Android Studio。
BufferedReader in = null;
try {
String urlStr = "https://blockchain.info/de/q/24hrprice";
URL url = new URL(urlStr);
URLConnection con = url.openConnection();
in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
String input;
while ((input = in.readLine()) != null) {
System.out.println(input); //you can parse input as Integer or Double now
}
} catch (MalformedURLException ex) {
System.err.println(ex);
} catch (IOException ex) {
System.err.println(ex);
} finally {
try {
in.close();
} catch (Exception ex) {
System.err.println(ex);
}
}
感謝您的快速響應!當我嘗試使用您的代碼時,出現以下錯誤: 新的URL(urlStr)錯誤:java.net.malformedurlexception url.openConnection,con.getInputStream,in.readLine和in.close錯誤:java.io. IOException 我在做什麼錯? – user3254327 2014-11-05 11:57:18
我不知道...它適用於我。你有沒有複製我更新的答案?我只是更改了urlStr,以包含「https://」,而我並沒有在第一個版本中使用它。 – vefthym 2014-11-05 12:00:50
等等...你把它放在try-catch塊嗎? – vefthym 2014-11-05 12:02:05
結果是什麼? – vefthym 2014-11-05 10:46:14
https://blockchain.info/de/q/24hrprice(=〜337.5)的結果。 – user3254327 2014-11-05 10:46:50