Android Nexus v4.0.2上的HttpURLConnection问题

时间:2012-01-19 01:27:16

标签: android

以下代码根本不适用于运行v4.0.2的Android Galaxy Nexus,它可以在模拟器和其他旧设备中使用。当在旧设备和emu上运行时,变量"是"根据需要得到所有字节,一切都很好。在Nexus上运行时,它会在"""和"是"保持为空。然后,当我尝试使用"是"在类的下方,它会抛出一个空指针,因为"是"一片空白。如何修复此文件未找到错误?该文件可在其他设备/ emu / browser上访问。

我收到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();
    } 

2 个答案:

答案 0 :(得分:3)

urlConnection.setDoOutput(真);

应该是:

urlConnection.setDoOutput(假);

答案 1 :(得分:0)

urlConnection.setDoOutput(true)有效地将方法更改为POST,因此您的服务器可能不响应POST?

HTTPUrlConnection确实有一个丑陋而混乱的界面。这是最近关于其特点的一篇文章:  http://www.tbray.org/ongoing/When/201x/2012/01/17/HttpURLConnection