下面的代碼根本無法在我的Android Galaxy Nexus上運行v4.0.2,它在仿真器和其他舊版設備中工作。當在較舊的設備上運行並且emu變量「is」正在根據需要獲取所有字節並且一切都很好。在Nexus上運行時,它會在「is」處引發未找到的異常,並且「is」保持爲空。然後,當我嘗試與「is」一起工作時,它會拋出一個空指針,因爲「is」爲空。我怎樣才能解決這個文件未找到錯誤?該文件可在其他設備/ emu /瀏覽器上訪問。我正在越來越java.io.FileNotFoundException:在is = urlConnection.getInputStream();我不知道這是什麼?Android Nexus v4.0.2上的HttpURLConnection問題
下面是代碼:
// GET
InputStream is = null;
try {
// set the URL that points to a file to be downloaded
URL url = new URL(downloadURL);
// create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
// set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
// connect and download
urlConnection.connect();
// used in reading the data from the internet
is = urlConnection.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
非常有趣。當調用setDoOutput()時,它應該是false而不是true。改變這似乎解決了這個問題。問題是爲什麼這隻影響這個設備/ os而不是每個其他設備/ os?嗯 – 2012-01-19 01:35:43
具有相同的行爲,但setDoOutput究竟如何影響和產生最終的io異常對我而言並不清楚。該方法被稱爲真,甚至沒有發送請求主體。改爲假解決了我的問題。 – 2012-02-14 15:02:18