1
public class Downloader {
public static String getWebPage(String URLString) {
String retVal = "";
try {
// Perform action on click
URL url = new URL(URLString);
URLConnection conn = url.openConnection();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = "";
while ((line = rd.readLine()) != null) {
retVal += line + "\n";
}
rd.close();
} catch (MalformedURLException e) {
// Print out the exception that occurred
retVal = "Invalid URL " + URLString + ": " + e.getMessage();
} catch (Exception e) {
retVal = e.getMessage();
}
return retVal;
}
}
上述代碼在仿真器2.3.1上運行良好。當我嘗試在模擬器3.x.x或我的Motorolla XOOM上執行相同的代碼時,代碼不起作用。它不會產生任何錯誤,但會返回空白文本。任何想法如何我可以解決這個問題?Android下載文件錯誤
注意:我已在清單文件中包含以下內容。
<uses-permission android:name="android.permission.INTERNET" />
是否讀什麼?你的代碼如何從循環中退出? (順便說一句,考慮使用StringBuilder進行字符串連接),爲什麼不使用Apache IOUtils.readLines? – Snicolas
你也應該打印堆棧跟蹤以防異常 – Snicolas
如果我將adroid版本設置爲2.3.1,它在模擬器上工作正常。當我嘗試在3.x.x版本的android上執行它時,問題就會出現,無論它是模擬器還是設備。如果在執行下列行時失敗 BufferedReader rd = new BufferedReader(new InputStreamReader( conn.getInputStream())); –