2012-09-08 90 views
0

我有這個代碼顯示html內容的問題。當我在智能手機上試用它時,我會打印出「錯誤」來捕捉錯誤,我錯在哪裏?Android java讀取html內容

String a2=""; 
try { 
    URL url = new URL("www.google.com"); 
    InputStreamReader isr = new InputStreamReader(url.openStream()); 
    BufferedReader in = new BufferedReader(isr); 
    String inputLine; 
    while ((inputLine = in.readLine()) != null){ 
     a2+=inputLine; 
    } 
    in.close(); 
    tx.setText("OUTPUT \n"+a2); 

} catch (Exception e) { 
    tx.setText("Error");  
} 
+1

...你如何告訴我們什麼是例外? –

回答

0

URL需要正確形成的url。您應該使用:

URL url = new URL("http://www.google.com"); 

更新:

當你得到一個NetworkOnMainThreadException,似乎您試圖使在主線程的連接。

這個解決方案是在AsyncTask中運行代碼。

+0

我試圖插入「http://」之前,但沒有做任何事情! – drKucho

+0

有必要擁有這種格式。您可能遇到另一個問題。加入'tx.setText( 「錯誤」 + E);' – Reimeus

+0

錯誤:android.os.NetworkOnMainThreadException – drKucho