Java Android版本上的FileNotFoundException>;2.3

Java Android版本上的FileNotFoundException>;2.3,java,android,inputstream,filenotfoundexception,Java,Android,Inputstream,Filenotfoundexception,我尝试将文件下载到手机上的SD卡。在安卓2.1、2.2和2.3上,一切都按预期运行,但在安卓4.0(emulator上的testen和我的Galaxy Nexus)上,它抛出一个FileNotFoundException 堆栈跟踪: 02-27 21:49:06.733: W/System.err(27648): java.io.FileNotFoundException: http://www.wdr.de/wdrlive/media/wdr5.m3u 02-27 21:49:06.733:

我尝试将文件下载到手机上的SD卡。在安卓2.1、2.2和2.3上,一切都按预期运行,但在安卓4.0(emulator上的testen和我的Galaxy Nexus)上,它抛出一个
FileNotFoundException

堆栈跟踪:

02-27 21:49:06.733: W/System.err(27648): java.io.FileNotFoundException: http://www.wdr.de/wdrlive/media/wdr5.m3u
02-27 21:49:06.733: W/System.err(27648):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
02-27 21:49:06.733: W/System.err(27648):    at de.arvidg.test.StartActivity$9.run(StartActivity.java:833)
02-27 21:49:06.733: W/System.err(27648):    at java.lang.Thread.run(Thread.java:856)

我下载文件的方法:

downloadFile("http://www.wdr.de/wdrlive/media/wdr5.m3u");

    public void downloadFile(final String fileUrl) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {

                    URL url = new URL(fileUrl);

                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                    urlConnection.setRequestMethod("GET");
                    urlConnection.setDoOutput(true);

                    urlConnection.connect();

//                  File cacheDir = appContext.getExternalCacheDir();
//                  File file = new File("/mnt/sdcard", "/cacheFile.ext");
                    File SDCardRoot = Environment.getExternalStorageDirectory();
                    File file = new File(SDCardRoot,"/cacheFile.ext");

                    if(!file.exists()) file.createNewFile();

                    FileOutputStream fileOutput = new FileOutputStream(file);

                    InputStream inputStream = urlConnection.getInputStream();

                    int totalSize = urlConnection.getContentLength();
                    int downloadedSize = 0;

                    byte[] buffer = new byte[1024];
                    int bufferLength = 0;

                    while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                        fileOutput.write(buffer, 0, bufferLength);
                        downloadedSize += bufferLength;
                        Log.d(TAG, "downloadedSize = " + downloadedSize + " | totalSize = " + totalSize);
//                      updateProgress(downloadedSize, totalSize);

                    }
                    fileOutput.close();

            } catch (MalformedURLException e) {
                    e.printStackTrace();
            } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }).start();
    }

简单删除
urlConnection.setDoOutput(true)


多亏了pfn#android dev

所以要回答最初的问题,当您从服务器收到错误消息,然后尝试访问
getInputStream()
时,还会出现
FileNotFoundException
。如果响应代码为400或更高,则对
getInputStream()
的任何调用都将抛出此错误。在这种情况下,您应该改为选中
getErrorStream()
。请参阅我的答案。

这不是导致问题的原因,但不应为缓存文件使用绝对文件名。试试这个:
File File=new文件(SDCardRoot,“cacheFile.ext”)(注意没有“/”。好的,谢谢。我肯定会的。有人知道错误的来源吗?检查响应代码urlConnection.getResponseCode();在处理输入流之前。谢谢405-不允许使用方法。有没有其他下载消息(“GET”似乎不起作用)。有人建议使用apache httpClient可以修复它,不确定它在您的情况下是否起作用,这里ICS,值得简单方便地回答+1,谢谢它解决了我的问题。很好,它起作用了。但是对于localhost IP,不使用10.0.2.2。显然,JAVA中的这一行强制http协议更改GET to a POST,而不管指定GET,但这给了我一个错误IO错误:不支持输出JAVA.net.protocol异常:不支持outputsame here System.err(9083):java.net.ProtocolException:方法不支持请求主体:GET