在Android上使用HttpURLConnection“IOException:BufferedInputStream已关闭”

时间:2013-07-30 08:11:09

标签: android sockets inputstream httpurlconnection ioexception

我正在尝试为Android构建3G连接速度测试

我遇到了Logcat中显示的IOException:

" IOException:BufferedInputStream已关闭"

以下是代码:

   OutputStream output = null;
    InputStream input = null;
    try{  
           URL url = new URL(myFullPathUrl);
           HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
           urlConnection.setDoInput(true);
           urlConnection.setDoOutput(true);


           output = urlConnection.getOutputStream();
           input = urlConnection.getInputStream();

    //note: the guy who worked on this project before me use socket, and he requested the file like this:
    //          output.write( ("GET " + filePath + " HTTP/1.0\r\n").getBytes() );
    //          output.write("Accept: */*\r\n".getBytes());
    //          output.write("\r\n".getBytes());
    // where filePath is something like "/directory/image.jpg"
    // and when he open the socket connecion, he use only host name like "my.domain.com"
    // But I changed his code to use HttpURLConnection instead and use myFullPathUrl =            
    // "my.domain.com/directory/image.jpg"

        catch (Exception e){
             e.printStackTrace();
        }finally {
        urlConnection.disconnect(); //edit 3 : this is the culprit. Sorry I forgot to include this. Please all accept my apology and let's see this question as my little contribution that you can't use the input/output stream object after you call .disconnect()
             }  
while ((DownloadedByte += input.read(BufferData,0,BufferData.length)) != -1)//*** Exception at this line
            {.......... //Connection speed test code here

根据Logcat,异常发生在while()循环的行上(我在上面标记了 *

我确认inputStream对象不为null。任何人都知道可能是什么原因?

谢谢!

最佳, 吉滴

编辑:根据要求提供完整的Logcat:

07-30 15:58:00.349: W/System.err(26626): java.io.IOException: BufferedInputStream is closed
07-30 15:58:00.349: W/System.err(26626):    at java.io.BufferedInputStream.streamClosed(BufferedInputStream.java:118)
07-30 15:58:00.359: W/System.err(26626):    at java.io.BufferedInputStream.read(BufferedInputStream.java:271)
07-30 15:58:00.359: W/System.err(26626):    at libcore.net.http.FixedLengthInputStream.read(FixedLengthInputStream.java:45)
07-30 15:58:00.359: W/System.err(26626):    at com.mycompany.speedtest.util.MyHttpDownloader.doInBackground(DownloadFileSocket.java:202)
07-30 15:58:00.359: W/System.err(26626):    at com.mycompany.speedtest.util.MyHttpDownloader.doInBackground(DownloadFileSocket.java:1)
07-30 15:58:00.359: W/System.err(26626):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-30 15:58:00.359: W/System.err(26626):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-30 15:58:00.359: W/System.err(26626):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-30 15:58:00.359: W/System.err(26626):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-30 15:58:00.359: W/System.err(26626):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-30 15:58:00.359: W/System.err(26626):    at java.lang.Thread.run(Thread.java:864)

编辑2:

我只是尝试调用input.available()来获取可以读取的估计字节数,并得到相同的异常... - * -

4 个答案:

答案 0 :(得分:2)

您在while循环中的条件不正确。 InputStream#read返回读取的字节数,如果在流的末尾,则返回-1。但是,您不断添加读取到DownloadedByte的字节数。请改用以下条件(注意=而不是+ =):

while ( ( bytesRead = inputStream.read( result, 0, result.length ) ) != -1 ) {
   // TODO
}

答案 1 :(得分:1)

我建议你把while循环的代码放到try块中。因为如果没有抛出异常,你只能处理这段代码。

如果你可以发布你正在获得的整个例子

会很好

祝福

答案 2 :(得分:1)

罪魁祸首是urlConnection.disconnect();

删除它,问题解决了。请参阅问题中的“编辑3”。谢谢大家!! =)

答案 3 :(得分:-2)

尝试设置超时:

urlConnection.setConnectTimeout(10000); //set timeout to 10 seconds