android - Android Nexus v4.0.2 上的 HttpURLConnection 问题

标签 android

下面的代码根本无法在运行 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/

相关文章:

android - 使用 Windows CLI 将 phonegap 应用程序安装到 android 模拟器

android - 导入安卓项目

java - 以编程方式启用通知 channel

java - 如何在 Android 中比较 AM/PM 时间

android - 如何在 android fragment 中使用 SQLite?

java - 使用 NumberFormat 将美分转换为欧元

android - 如何在 Android 中单击按钮时启用 BroadcastReceiver?

android - 更平滑的 Android 边框半径?

android - 具有 layout_weight 属性和 src 的 imageView 无法适应给定的尺寸

android - 警告 : This AsyncTask class should be static or leaks might occur