下面的代码根本无法在运行 v4.0.2 的 Android Galaxy Nexus 上运行,但可以在模拟器和其他旧设备中运行。当在旧设备和 emu 上运行时,变量“is”会根据需要获取所有字节,一切都很好。在 Nexus 上运行时,它会在“is”处抛出文件未找到异常,并且“is”保持为空。然后,当我尝试在类中进一步使用“is”时,它会抛出一个空指针,因为“is”为空。如何修复此文件未找到错误?该文件可在其他设备/emu/浏览器上访问。
我收到 java.io.FileNotFoundException: at is = urlConnection.getInputStream();
这是代码:
// 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();
}
最佳答案
urlConnection.setDoOutput(true);
应该是:
urlConnection.setDoOutput(false);
关于android - Android Nexus v4.0.2 上的 HttpURLConnection 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8919948/